How do I POST JSON data with cURL?

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

    You need to set your content-type to application/json. But -d sends the Content-Type application/x-www-form-urlencoded, which is not accepted on Spring's side.

    Looking at the curl man page, I think you can use -H:

    -H "Content-Type: application/json"
    

    Full example:

    curl --header "Content-Type: application/json" \
      --request POST \
      --data '{"username":"xyz","password":"xyz"}' \
      http://localhost:3000/api/login
    

    (-H is short for --header, -d for --data)

    Note that -request POST is optional if you use -d, as the -d flag implies a POST request.


    On Windows, things are slightly different. See the comment thread.

提交回复
热议问题