Search code examples
scalasprayspray-jsonspray-client

Json media types for requests in Scala Spray


I have a simple json app. It works fine if I make a request like

curl -X POST --data "userId=1" http://localhost:8080/register

But request fails when I try to make a json request, e.g.

curl -v -H "Content-type: application/json" -X POST -d '{"userId"=1}' http://localhost:8080/register

> Content-type: application/json
>
< HTTP/1.1 415 Unsupported Media Type
< Server: spray-can/1.3.3
< Date: Mon, 07 Sep 2015 14:37:29 GMT
< Content-Type: text/plain; charset=UTF-8
<

Expected 'application/x-www-form-urlencoded' or Expected 'multipart/form-data'

How to ask Spray to process json request?


Solution

  • Your curl command looks correct except for this part: '{"userId"=1}'. This is not a valid JSON document. It should be this instead '{"userId" : 1}'. I think that's why you getting this error.