How do I POST JSON data with cURL?

后端 未结 24 2569
刺人心
刺人心 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:36

    It worked for me using:

    curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":100}' http://localhost/api/postJsonReader.do
    

    It was happily mapped to the Spring controller:

    @RequestMapping(value = "/postJsonReader", method = RequestMethod.POST)
    public @ResponseBody String processPostJsonData(@RequestBody IdOnly idOnly) throws Exception {
            logger.debug("JsonReaderController hit! Reading JSON data!"+idOnly.getId());
            return "JSON Received";
    }
    

    IdOnly is a simple POJO with an id property.

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

    I know, a lot has been answered to this question but wanted to share where I had the issue of:

    curl -X POST http://your-server-end-point -H "Content-Type: application/json" -d @path-of-your-json-file.json

    See, I did everything right, Only one thing - "@" I missed before the JSON file path.

    I found one relevant go-to document on internet - https://gist.github.com/subfuzion/08c5d85437d5d4f00e58

    Hope that might help the few. thanks

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

    As an example, create a JSON file, params.json, and add this content to it:

    [
        {
            "environment": "Devel",
            "description": "Machine for test, please do not delete!"
        }
    ]
    

    Then you run this command:

    curl -v -H "Content-Type: application/json" -X POST --data @params.json -u your_username:your_password http://localhost:8000/env/add_server
    
    0 讨论(0)
  • 2020-11-22 00:40

    You could also put your JSON content in a file and pass it to curl using the --upload-file option via standard input, like this:

     echo 'my.awesome.json.function({"do" : "whatever"})' | curl -X POST "http://url" -T -
    
    0 讨论(0)
  • 2020-11-22 00:43

    You can use Postman with its intuitive GUI to assemble your cURL command.

    1. Install and Start Postman
    2. Type in your URL, Post Body, Request Headers etc. pp.
    3. Click on Code
    4. Select cURL from the drop-down list
    5. copy & paste your cURL command

    Note: There are several options for automated request generation in the drop-down list, which is why I thought my post was neccessary in the first place.

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

    This worked well for me, additionally using BASIC authentication:

    curl -v --proxy '' --basic -u Administrator:password -X POST -H "Content-Type: application/json"
            --data-binary '{"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://httpbin.org/post
    

    Of course, you should never use BASIC authentication without SSL and a checked certificate.

    I ran into this again today, using Cygwin's cURL 7.49.1 for Windows... And when using --data or --data-binary with a JSON argument, cURL got confused and would interpret the {} in the JSON as a URL template. Adding a -g argument to turn off cURL globbing fixed that.

    See also Passing a URL with brackets to curl.

    0 讨论(0)
提交回复
热议问题