How do I POST JSON data with cURL?

后端 未结 24 2568
刺人心
刺人心 2020-11-21 23:56

I use Ubuntu and installed cURL on it. I want to test my Spring REST application with cURL. I wrote my POST code at the Java side. However, I want to test it with cURL. I am

相关标签:
24条回答
  • 2020-11-22 00:48

    Please check this tool. It helps you to easily create curl snippets.

    curl -XGET -H "Accept: application/json" -d "{\"value\":\"30\",\"type\":\"Tip 3\",\"targetModule\":\"Target 3\",\"configurationGroup\":null,\"name\":\"Configuration Deneme 3\",\"description\":null,\"identity\":\"Configuration Deneme 3\",\"version\":0,\"systemId\":3,\"active\":true}" "http://localhost:8080/xx/xxx/xxxx"
    
    0 讨论(0)
  • 2020-11-22 00:50

    If you're testing a lot of JSON send/responses against a RESTful interface, you may want to check out the Postman plug-in for Chrome (which allows you to manually define web service tests) and its Node.js-based Newman command-line companion (which allows you to automate tests against "collections" of Postman tests.) Both free and open!

    0 讨论(0)
  • 2020-11-22 00:51

    You might find resty useful: https://github.com/micha/resty

    It's a wrapper round CURL which simplifies command line REST requests. You point it to your API endpoint, and it gives you PUT and POST commands. (Examples adapted from the homepage)

    $ resty http://127.0.0.1:8080/data #Sets up resty to point at your endpoing
    $ GET /blogs.json                  #Gets http://127.0.0.1:8080/data/blogs.json
                                       #Put JSON
    $ PUT /blogs/2.json '{"id" : 2, "title" : "updated post", "body" : "This is the new."}'
                                       # POST JSON from a file
    $ POST /blogs/5.json < /tmp/blog.json
    

    Also, it's often still necessary to add the Content Type headers. You can do this once, though, to set a default, of add config files per-method per-site: Setting default RESTY options

    0 讨论(0)
  • 2020-11-22 00:51

    HTTPie is a recommended alternative to curl because you can do just

    $ http POST http://example.com/some/endpoint name=value name1=value1
    

    It speaks JSON by default and will handle both setting the necessary header for you as well encoding data as valid JSON. There is also:

    Some-Header:value
    

    for headers, and

    name==value
    

    for query string parameters. If you have a large chunk of data, you can also read it from a file have it be JSON encoded:

     field=@file.txt
    
    0 讨论(0)
  • 2020-11-22 00:51

    You can pass the extension of the format you want as the end of the url. like http://localhost:8080/xx/xxx/xxxx.json

    or

    http://localhost:8080/xx/xxx/xxxx.xml

    Note: you need to add jackson and jaxb maven dependencies in your pom.

    0 讨论(0)
  • 2020-11-22 00:54

    Using CURL Windows, try this:

    curl -X POST -H "Content-Type:application/json" -d "{\"firstName\": \"blablabla\",\"lastName\": \"dummy\",\"id\": \"123456\"}" http-host/_ah/api/employeeendpoint/v1/employee
    
    0 讨论(0)
提交回复
热议问题