Rohit
Rohit

Reputation: 2152

cURL POST to rest endpoint cause error at server

I am trying to make a POST request to my locally running rest endpoint as follows:

curl -X POST -d '[{"name":"xyz","cost":"10"}]' http://localhost:8080/xyz/rest/abc --header "Content-Type: application/json"

this results in

Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@3180b68d; line: 1, column: 2]

When I use other tool for eg. RESTClient plugin in Firefox browser I get an expected response for this post body from my rest endpoint.

Well I enabled server side logging of the request at my Jersey rest endpoint: The RESTClient request results in:

15:48:31,916 INFO  [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-1) 1 * Server has received a request on thread http--127.0.0.1-8080-1
1 > POST http://localhost:8080/xyz/rest/abc
1 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
1 > accept-encoding: gzip, deflate
1 > accept-language: null
1 > cache-control: no-cache
1 > connection: keep-alive
1 > content-length: 91
1 > content-type: application/json; charset=UTF-8
1 > host: localhost:8080
1 > pragma: no-cache
1 > user-agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Fire
fox/40.0

15:48:32,086 INFO  [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-1) 1 * Server responded with a response on thread http--127.0.0.1-8080-1
1 < 201
1 < Content-Type: application/json

The cURL request results in:

15:49:04,786 INFO  [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-2) 2 * Server has received a request on thread http--127.0.0.1-8080-2
2 > POST http://localhost:8080/xyz/rest/abc
2 > accept: application/json
2 > content-length: 76
2 > content-type: application/json
2 > host: localhost:8080
2 > user-agent: curl/7.33.0

15:49:04,792 INFO  [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-2) 2 * Server responded with a response on thread http--127.0.0.1-8080-2
2 < 400
2 < Content-Type: text/plain

Now, I know my rest endpoint works correcty, Just curious as to why this is happening for cURL request. Any suggestions are welcome.

Upvotes: 0

Views: 896

Answers (1)

Rohit
Rohit

Reputation: 2152

OK so the problem is that I am using a windows machine and so single quote around the data is causing this issue.

To get it to work i had to use double quotes around the data and escape the double quotes within the data.

curl -X POST -d "[{\"name\":\"xyz\",\"cost\":\"10\"}]" http://localhost:8080/xyz/rest/abc --header "Content-Type: application/json"  

Upvotes: 3

Related Questions