2018年7月12日 星期四

[Linux 常見問題] How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

Source From Here 
Question 
I use Ubuntu and installed Curl on it. I want to test my Spring REST application with Curl. I wrote my POST code at Java side. However, I want to test it with Curl. I am trying to post a JSON data. 

Any ideas about the right format of the Curl command? 

How-To 
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
  1. -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

From above, -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.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...