728x90
반응형

Echo로 개발하여 API로 통신하는데 Content-Type: text/plain으로 통신을 해야했다.

func(c echo.Context) (err error) {
  body := &Body{}
  if err = c.Bind(body); err != nil {
    return
  }
  return c.JSON(http.StatusOK, body)
}

위와 같이 echo.Context.Bind를 이용하여 JSON 형식을 파싱하여 사용하고 있었는데

 

Content-Type: text/plain인 경우 415 Unsupported Media Type 에러가 발생했다.

 

말 그대로 지원하지 않는 미디어 형식이라 에러가 난 것이다.

 

이를 해결하기 위해서 다음과 같이 변경하면 Content-Type에 무관하게 동작하도록 할 수 있다.

func(c echo.Context) (err error) {
  rBody := &RegisterBody{}
  if b, err := ioutil.ReadAll(c.Request().Body); err == nil {
    if err := json.Unmarshal(b, &body); err != nil {
        return
    }
  }
  return c.JSON(http.StatusOK, body)
}

 

참고 문헌

  1. https://github.com/labstack/echo/issues/156#issuecomment-124684206

반응형

'Golang' 카테고리의 다른 글

[Golang] Go의 선(The Zen of Go)  (0) 2020.06.04
[Golang] Kafka 연동 문제  (0) 2020.04.30
[Golang] Go를 사용하면서 발생했던 문제들  (0) 2020.04.23
[Golang] Java gzip migration  (0) 2020.01.30
[Golang] 410 Gone  (0) 2020.01.25

+ Recent posts