Rest API Content-Type header question #47849
Replies: 4 comments 4 replies
-
/cc @geoand (kotlin) |
Beta Was this translation helpful? Give feedback.
-
When using the hibernate-orm-quickstart and doing a POST to http://localhost:8080/fruits with an empty body, I don't get HTTP 415, so what you are seeing in not a general handling of empty body |
Beta Was this translation helpful? Give feedback.
-
Even though the method accepts a nullable body, the server still expects a To solve the issue, I would recommend method overloading. Handle POST with no body@POST
@Path("/")
@Blocking
fun myPostApiNoBody(): RestResponse<Unit> Handles POST with
|
Beta Was this translation helpful? Give feedback.
-
If the client/API testing tool sees an HTTP body has content, then the tool adds Content-Type to the HTTP header. POST /save HTTP/1.1
Host: example.com
Content-Type: application/json
{
"id": 123,
"firstName": "Paul",
"lastName": "Klee",
"email": "p.klee@example.com"
} If there is no content in the body and Content-Type is set to some value, that may result in a 415 (Unsupported Media Type ) error. POST /save HTTP/1.1
Host: example.com
Content-Type: application/json Since the Content-Type header is set server tries to parse it. Since an empty body is not a valid JSON server, return a 415 error. If there is no body and no Content-Type header server treats it as no content. POST /save HTTP/1.1
Host: example.com Tip If you want to accept any type use Warning Use it carefully Note Here are the few Content-Type headers that may be helpful.
Note You can find more about Content-Type in this MDN docs |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have an existed POST API, now I want to add a body for it. To be backward compatible, the body is optional.
Then when I perform an API call to it without a body, it throw HTTP 415 from both PostMan and frontend.
Can you please explain why? Thank you
Beta Was this translation helpful? Give feedback.
All reactions