《RESTful Web APIs》書中有一段POST API示例:
I then send the filled-out template as part of an HTTP POST request:
POST /api/ HTTP/1.1
Host: www.youtypeitwepostit.com
Content-Type: application/vnd.collection+json
{ "template":
{
"data": [
{"prompt": "Text of the message", "name": "text", "value": "Squid!"}
]
}
}
(Note that my request’s Content-Type is application/vnd.collection+json . This
filled-out template is a valid Collection+JSON document all on its own.)
The server responds:
HTTP/1.1 201 Created
Location: http://www.youtypeitwepostit.com/api/47210977342911065
作者Leonard Richardson and Mike Amundsen只說他們send了一個POST HTTP REQUEST,但我們不知道如何send?使用何種工具send?
step1. 我嘗試使用“在線HTTP GET/POST模擬請求測試工具”,以失敗告終;
step2. 在gdg的羣裏,偶然發現有朋友使用curl發送post請求至webapi,遂下載curl for windows,仿照他的命令測試,如下所示:
curl -X POST -H "Content-Type:application/vnd.collection+json" -d ‘{ "template" : { "data": [ { "prompt": "Text of the message", "name": "text", "value": "Nathan" } ] } }’ http://www.youtypeitwepostit.com/api/
命令執行後返回:
curl: (6) Could not resolve host: template
curl: (7) Failed connect to :80; No error
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: data
curl: (3) [globbing] bad range specification in column 2
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: prompt
curl: (6) Could not resolve host: Text of the message,
curl: (6) Could not resolve host: name
curl: (6) Could not resolve host: text,
curl: (6) Could not resolve host: value
curl: (6) Could not resolve host: Nathan
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 1
{"collection":{"version":"1.0","href":"http://www.youtypeitwepostit.com/api/","error":{"title":"Server Error","code":500}}}
命令返回結果與作者在書裏寫的不一樣,這是爲什麼呢?
經過一番周折,我在這位仁兄的博客裏找到了答案:http://blog.csdn.net/lipei1220/article/details/8536520
step3. 成功模擬作者的發送命令,成功測試人生中第一個POST WEB API,cool! 感謝gdg友人的分享討論。
curl -X POST -H Content-Type:application/vnd.collection+json -d "{ \"template\" : { \"data\": [ { \"prompt\": \"Text of the message\", \"name\": \"text\", \"value\": \"Nathan\" } ] } }" http://www.youtypeitwepostit.com/api/
如果你也在閱讀《RESTful Web APIs》,如果你也想try it yourself on your windows platform,just download curl for windows and try it!
===
2016年4月8日追加:
1. 如果希望看到上述命令返回的結果,需要打開curl命令的-i開關,如下所示:
curl -i -X POST -H Content-Type:application/vnd.collection+json -d "{ \"template\" : { \"data\": [ { \"prompt\": \"Text of the message\", \"name\": \"text\", \"value\": \"Nathan\" } ] } }" http://www.youtypeitwepostit.com/api/
執行命令後返回結果爲:
HTTP/1.1 201 Created
Server: Cowboy
Connection: keep-alive
Location: http://www.youtypeitwepostit.com/api/9239483117125928
Date: Fri, 08 Apr 2016 07:33:17 GMT
Transfer-Encoding: chunked
Via: 1.1 vegur
2. 如果希望看到一次http通信的整個過程,包括端口連接和http request頭信息,則需要打開curl命令的-v開關,如下所示:
curl -v -X POST -H Content-Type:application/vnd.collection+json -d "{ \"template\" : { \"data\": [ { \"prompt\": \"Text of the message\", \"name\": \"text\", \"value\": \"Nathan\" } ] } }" http://www.youtypeitwepostit.com/api/
執行命令後返回結果爲:
* Adding handle: conn: 0x2225f90 * Adding handle: send: 0 * Adding handle: recv: 0 * Curl_addHandleToPipeline: length: 1 * - Conn 0 (0x2225f90) send_pipe: 1, recv_pipe: 0 * About to connect() to www.youtypeitwepostit.com port 80 (#0) * Trying 107.21.92.176... * Connected to www.youtypeitwepostit.com (107.21.92.176) port 80 (#0) > POST /api/ HTTP/1.1 > User-Agent: curl/7.33.0 > Host: www.youtypeitwepostit.com > Accept: */* > Content-Type:application/vnd.collection+json > Content-Length: 104 > * upload completely sent off: 104 out of 104 bytes < HTTP/1.1 201 Created * Server Cowboy is not blacklisted < Server: Cowboy < Connection: keep-alive < Location: http://www.youtypeitwepostit.com/api/20962976710870862 < Date: Fri, 08 Apr 2016 07:38:36 GMT < Transfer-Encoding: chunked < Via: 1.1 vegur < * Connection #0 to host www.youtypeitwepostit.com left intact