2012-08-06 131 views
45

我的問題: 使用命令行工具在我的POST請求發送一些數據的同時捲曲我的本地主機服務器不起作用。WINDOWS RESTful服務的cURL POST命令行

什麼似乎會導致錯誤: 想象這樣的事情

  1. curl -i -X POST -H 'Content-Type: application/json' -d '{"data1": "data goes here", "data2": "data2 goes here"}' http:localhost/path/to/api

返回數據的結果

curl: (6) Could not resolve host: application; No data record of requested type 
curl: (6) Could not resolve host: data goes here,; No data record of requested type 
curl: (6) Could not resolve host: data2; No data record of requested type 
curl: (3) [globbing] unmatched close brace/bracket at pos 16 

經過一番搜索我想通了那個問題不可能是用於t的sintax他請求,因爲它在UNIX shell上工作。

Are you possibly using Windows? That so looks like a completely broken shell that doesn't properly deal with single-quotes vs double-quotes. I just tried that command line and it worked fine on my linux box. http://curl.haxx.se/mail/archive-2011-03/0066.html

我試圖解決與 「逃了\」,但它仍然沒有奏效

2.

curl -i -X POST -H 'Content-Type: application/json' -d '{\"data1\": \"data goes here\", \"data2\": \"data2 goes here\"}' http: //localhost/path/to/api

3.

curl -i -X POST -H 'Content-Type: application/json' -d '{\"data1\": \"data goes here\", \"data2\": \"data2 goes here\"}' http: //localhost/path/to/api

所以我放棄。 的Windows似乎與POST發送的JSON對象搞亂

+0

請不要在問題標題中加上[[解析]]。如果你已經想出了你的問題的答案,你應該把它作爲答案。 – 2012-08-06 23:20:01

+1

感謝@CodyGray的警告。我會糾正我的帖子。 – Lothre1 2012-08-06 23:35:22

+1

如果你的機器上安裝了git,你可以使用git bash來捲曲。它可以節省很多頭痛。 – 2017-06-28 21:03:31

回答

83

我跑進我的X64 Win7的筆記本電腦同樣的問題,並能得到它的工作使用由使用非常類似的命令行格式標記Win64 - Generic w SSL捲曲發佈:

C:\Projects\curl-7.23.1-win64-ssl-sspi>curl -H "Content-Type: application/json" -X POST http://localhost/someapi -d "{\"Name\":\"Test Value\"}" 

其中僅不同從你的第二個轉義版本通過在轉義字符和標題參數值周圍使用雙引號。絕對更喜歡linux shell語法。

13

替代解決方案:不是命令行更多用戶友好的解決方案:

If you are looking for a user friendly way to send and request data using HTTP Methods other than simple GET's probably you are looking for a chrome extention just like this one http://goo.gl/rVW22f called AVANCED REST CLIENT

對於球員希望留在命令行我推薦的cygwin :

I ended up installing cygwin with CURL which allow us to Get that Linux feeling - on Windows!

Using Cygwin command line this issues have stopped and most important, the request syntax used on 1. worked fine.

相關鏈接:

Where i was downloading the curl for windows command line?

For more information on how to install and get curl working with cygwin just go here

我希望它可以幫助別人,因爲我整個早上都在這上面。

+0

這不應該被接受的答案。 OP沒有要求提供基於Web的REST客戶端或替代命令行客戶端的建議。他們要求幫助逃脫糾正捲曲,這是由user1683523回答。 – 2015-05-29 19:46:59

+0

但是,對於許多問這個問題的人來說,這是一個更好的答案。 – RickAndMSFT 2016-06-23 23:10:48

35

命令行的另一種方式比引用引號更容易,它將json放入文件中,並使用curl參數的@前綴,例如,在json中使用以下內容。TXT:

{ "syncheader" : { 
    "servertimesync" : "20131126121749", 
    "deviceid" : "testDevice" 
    } 
} 

然後在我的情況下,我發出:

curl localhost:9000/sync -H "Content-type:application/json" -X POST -d @json.txt 

保持JSON更加易讀了。

+0

你是一個拯救生命的人! – 2014-05-01 19:16:00

+0

這一個工作,thx! – kukudas 2014-11-05 20:34:37

+0

哦,甜甜的羅納德麥克唐納謝謝你 – 2015-06-09 08:36:40

0
  1. 嘗試使用雙引號(「),而不是單個的人(')。
  2. 爲了保持JSON格式的報價,試戴倍增( 」「)。
  3. 爲了保護內部的數據報價,嘗試以雙逃脫他們像這樣(\\ 「」)。

    curl ... -d "{""data1"": ""data1 goes here"", ""data2"": ""data2 goes here""}" 
    curl ... -d "{""data"": ""data \\""abc\\"" goes here""}" 
    
2

至少在我測試Windows的二進制版本,(該Generic Win64 no-SSL binary,目前基於7.33.0) ,您在解析命令行參數方面受到限制。 xmas的答案描述了該設置中的正確語法,該語法也適用於批處理文件。使用提供的示例:

curl -i -X POST -H "Content-Type: application/json" -d "{""data1"":""data goes here"",""data2"":""data2 goes here""}" http:localhost/path/to/api 

的清潔替代,以避免處理轉義字符,這取決於什麼庫用來解析命令行,是在一個單獨的文件的標準JSON格式文本:

curl -i -X POST -H "Content-Type: application/json" -d "@body.json" http:localhost/path/to/api 
+1

謝謝。它並不重要,但現在無論是Windows 10還是我安裝的curl版本,我現在必須在-d之後引用我的json文件參數引號,否則行結尾會將其搞亂。 – joezen777 2016-03-23 18:52:56