how to post with curl to REST/JSON service?

I have tried out the sample REST JSON service at

http://www.javacodegeeks.com/2013/04/spring-mvc-easy-rest-based-json-services-with-responsebody.html

and the JQuery client can successfully post a person object to the service! Yahoo!

How do I do the same thing with cURL? Here is my attempt:

curl -i -X POST -H "Content-Type: application/json; charset=UTF-8" -H "Accept: application/json" -d "{'name':'siegfried','age':26}" http://localhost:8080/api/person
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    71  100    42  100    29     39     26  0:00:01  0:00:01 --:--:--    40
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Content-Length: 42
Server: Jetty(6.1.25)

Saved person: Person [name=null, age=null]
Compilation finished at Sun Dec 08 22:14:05

It is not parsing the data! How do I make it parse my json data?

I've been google searching for hours and trying out many combinations of escaping quotes and apostrophes and the like and nothing seems to work.

Thanks

siegfried


The -d will send your data as a post, you do not need to declare POST .

man curl gives the description on how to use -d . If anyone else would need it.

A simple Sinatra server http://www.sinatrarb.com, or something like it, can be used to debug your curls as well as a mock server

Is there a possibility that the problem is on the server-side, if you run it on local host you should have access to it, right?


I found the problem! The spring @ResponseBody uses JSON for get but uses URL Encoding for POST. See the jQuery POST method for http://codetutr.com/2013/04/09/spring-mvc-easy-rest-based-json-services-with-responsebody/ Why the inconsistency? This drove me nuts!

Well, I still don't have a cURL command. But I do have java and groovy clients that can now POST and GET. I should have a cURL soon.

链接地址: http://www.djcxy.com/p/8594.html

上一篇: 如何获得来自其他API回调调用的响应

下一篇: 如何使用curl发布到REST / JSON服务?