2017-08-04 131 views
0

如果我發出curl命令給REST api,那麼我會得到下面的響應。使用curl命令時,http POST resquest的數據格式

curl -i http://10.4.0.22:8088/api/clients/TEST_1/8194/1/1 

響應:

HTTP/1.1 200 OK 
Content-Type: application/json 
Content-Length: 51 
Server: Jetty(9.1.z-SNAPSHOT) 

{"status":"CONTENT","content":{"id":1,"value":"0"}} 

我的理解,這相當於一個HTTP請求GET

我想要做的是更新value字段並將值更改爲1

我認爲這需要POST請求使用-d標誌。

我的問題是我怎麼知道數據應該在curl命令中的格式是什麼?

我試圖

curl -d "{"status":"CONTENT","content":{"id":1,"value":"1"}}" http://10.4.0.22:8088/api/clients/EST_1/8194/1/1 

,但我得到這個答覆。

{"status":"METHOD_NOT_ALLOWED"} 

我認爲我指定json之後te的方式是-d是不正確的嗎?

回答

0

響應METHOD_NOT_ALLOWED表示服務器不允許POST方法。

您可以添加一個-v以獲得詳細的響應輸出。有時服務器會回覆郵件中的哪些內容,它允許使用哪些內容。

我在我的應用程序端點上試過了你的參數,它工作。見下文

curl -d "{"status":"CONTENT","content":{"id":1,"value":"1"}}" http://sitename.azurewebsites.net/index.php -v 
* Adding handle: conn: 0x2c257d0 
* Adding handle: send: 0 
* Adding handle: recv: 0 
* Curl_addHandleToPipeline: length: 1 
* - Conn 0 (0x2c257d0) send_pipe: 1, recv_pipe: 0 
* About to connect() to sitename.azurewebsites.net port 80 (#0) 
* Trying 104.211.224.252... 
* Connected to sitename.azurewebsites.net (104.211.224.252) port 80 (#0) 
> POST /index.php HTTP/1.1 
> User-Agent: curl/7.33.0 
> Host: sitename.azurewebsites.net 
> Accept: */* 
> Content-Length: 39 
> Content-Type: application/x-www-form-urlencoded 
> 
* upload completely sent off: 39 out of 39 bytes 
< HTTP/1.1 200 OK 
< Content-Length: 0 
< Content-Type: text/html 
< X-Powered-By: PHP/5.4.45 
< Date: Fri, 04 Aug 2017 14:24:08 GMT 

您也可以嘗試在您的要求指定Content-Type頭並將其設置爲相應Mime值。

對於捲曲,你可以用你見過這個線程設置爲"Content-Type: application/json"

值,用-HHow to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

+0

非常感謝你的幫助的答覆。 –