《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

最新文章

  1. SharePoint 2013 图文开发系列之可视化WebPart
  2. 自动化服务安装部署工具-Ansible
  3. 与String有关的强制转换
  4. 百度地图API功能集锦
  5. Javascript Function()中的降龙十八掌
  6. BZOJ4527: K-D-Sequence 线段树
  7. ie8 table td拆分宽度不适应问题
  8. 都是以父元素的width为参照物的
  9. Codeforces Round #133 (Div. 2)
  10. 【JavaScript】JS跨域设置和取Cookie
  11. find命令小结
  12. Android 数据传输之MessagePack使用
  13. Python高级之Socket 探索(五)
  14. The 15th UESTC Programming Contest Preliminary G - GC?(X,Y) cdoj1564
  15. DotNetCore + Sonar + Coverlet 代码覆盖率检查
  16. Python-数据类型之数字
  17. Jet.com
  18. JAVAC 命令详解
  19. 全网最详细的大数据集群环境下如何正确安装并配置多个不同版本的Cloudera Hue(图文详解)
  20. MongoDB单机, 主从, 分布式部署

热门文章

  1. Spring Boot 如何快速集成 Redis 哨兵?
  2. Android开发之去掉listview的点击效果,一行代码间接粗暴,解决你的问题。
  3. 热更新 &amp;&amp; 增量更新
  4. Spine学习五- spine动画融合
  5. 转载:人家编写的程序:「雀神 AI」Suphx
  6. logback1.11的一个bug:有线程持续不断写入log文件,log文件就不会按设定以日期切换。
  7. rabbitmq集成和实战
  8. composer 国内镜像
  9. Navicat 闲置时间过长会卡死
  10. Linux:安装php