spring-guides/tut-rest

415 when attempting to post json object

Closed this issue · 2 comments

Hey I am new to Spring and I was having trouble posting to localhost:8080/employees from the command line...

When attempting to post:

curl -v -X POST localhost:8080/employees -H 'Content-Type:application/json' -d '{"name": "Samwise Gamgee", "role": "gardener"}'

I get this response:

"status":415,"error":"Unsupported Media Type","message":"Content type 'application/x-www-form urlencoded;charset=UTF-8' not supported"

This is the code that I have:

@PostMapping("/employees")
   ResponseEntity<?> newEmployee(@RequestBody Employee newEmployee) throws URISyntaxException {
       EntityModel<Employee> entityModel = assembler.toModel(repository.save(newEmployee));

       return ResponseEntity
               .created(entityModel.getRequiredLink(IanaLinkRelations.SELF).toUri())
               .body(entityModel);
   }

If anyone has any suggestions as to what might be going wrong I would really appreciate it. I apologize if this is the wrong place to ask this type of question, but I don't know what else to do.

I ran into this same issue.

When using cURL, I get the same error you did.

However, I was able to run the equivalent request using Postman and it succeeded. Seems like a funkiness with cURL is at play.

Ooh, I seem to have gotten it!

If you're using Windows it looks like you need to

  • replace the outer single quotes (') with double quotes (") and
  • escape any inner double quotes (") within the JSON
    • to escape, replace all " with \"

Doing so results in the following command:

curl -v -X POST localhost:8080/employees -H "Content-Type: application/json" -d "{\"name\": \"Samwise Gamgee\", \"role\": \"gardener\"}"

then you'll get a response something like:

Note: Unnecessary use of -X or --request, POST is already inferred.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> POST /employees HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 46
>
} [46 bytes data]
* upload completely sent off: 46 out of 46 bytes
< HTTP/1.1 201
< Location: http://localhost:8080/employees/6
< Content-Type: application/hal+json
< Transfer-Encoding: chunked
< Date: Fri, 08 May 2020 06:54:42 GMT
<
{ [221 bytes data]
100   256    0   210  100    46    210     46  0:00:01 --:--:--  0:00:01  4063
* Connection #0 to host localhost left intact
{
    "id": 6,
    "firstName": "Samwise",
    "lastName": "Gamgee",
    "role": "gardener",
    "name": "Samwise Gamgee",
    "_links": {
        "self": {
            "href": "http://localhost:8080/employees/6"
        },
        "employees": {
            "href": "http://localhost:8080/employees"
        }
    }
}

...or just use Postman or something similar. 🤷‍♂️

This is the StackOverflow answer that helped me solve it.