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)
}