《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

《RESTful Web APIs》书中有一段POST API示例,现实中我们如何测试这个示例?书中没有说,Let's try it!的更多相关文章

  1. 《RESTful Web APIs中文版》

    <RESTful Web APIs中文版> 基本信息 原书名:RESTful Web APIs 原出版社: O'Reilly Media 作者: Leonard Richardson    ...

  2. 前端Web APIS

    day01 - Web APIs 学习目标: 能够通过ID来获取元素能够通过标签名来获取元素能够通过class来获取元素能够通过选择器来获取元素能够获取body和html元素能够给元素注册事件能够修改 ...

  3. 使用JAX-RS创建RESTful Web Service

    guice resteasy http://www.cnblogs.com/ydxblog/p/7891224.html http://blog.csdn.net/withiter/article/d ...

  4. 用Jersey为Android客户端开发Restful Web Service

    平时在做Android客户端的时候经常要与服务器之间通信,客户端通过服务端提供的接口获取数据,然后再展示在客户端的界面上,作为Android开发者,我们平时更多的是关注客户端的开发,而对服务端开发的关 ...

  5. 《RESTful Web Services》第二章 识别资源

    引言:开放RESTful Web服务的首要步骤之一是设计资源模型. 2.1 如何从领域名词中识别资源   2.2 如何选择资源粒度     一些因素会影响数据库表和对象模型的设计,例如领域建模.需要高 ...

  6. 我所理解的RESTful Web API [Web标准篇]

    REST不是一个标准,而是一种软件应用架构风格.基于SOAP的Web服务采用RPC架构,如果说RPC是一种面向操作的架构风格,而REST则是一种面向资源的架构风格.REST是目前业界更为推崇的构建新一 ...

  7. 我所理解的RESTful Web API [设计篇]

    <我所理解的RESTful Web API [Web标准篇]>Web服务已经成为了异质系统之间的互联与集成的主要手段,在过去一段不短的时间里,Web服务几乎清一水地采用SOAP来构建.构建 ...

  8. 使用 Spring 3 来创建 RESTful Web Services

    来源于:https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/ 在 Java™ 中,您可以使用以下几种方法来创建 RESTful We ...

  9. 使用 Spring 3 来创建 RESTful Web Services(转)

    使用 Spring 3 来创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参 ...

随机推荐

  1. 巧用HashMap一行代码统计单词出现次数

    简介 JDK是在一直在迭代更新的,很多我们熟悉的类也悄悄的添加了一些新的方法特性.比如我们最常用的HashMap. 今天给大家讲一下HashMap在JDK8中添加的两个新方法compute和merge ...

  2. JavaScript 究竟是怎样去执行的?

    摘要: 理解 JS 引擎运行原理. 作者:前端小智 原文:搞懂 JavaScript 引擎运行原理 Fundebug经授权转载,版权归原作者所有. 一些名词 JS 引擎 — 一个读取代码并运行的引擎, ...

  3. 初学WebGL引擎-BabylonJS:第6篇-碰撞交错与挑选

    [playground]-collisions(碰撞) 先贴官方源码(机器翻译版本) var createScene = function () { var scene = new BABYLON.S ...

  4. 算法专题 | 10行代码实现的最短路算法——Bellman-ford与SPFA

    今天是算法数据结构专题的第33篇文章,我们一起来聊聊最短路问题. 最短路问题也属于图论算法之一,解决的是在一张有向图当中点与点之间的最短距离问题.最短路算法有很多,比较常用的有bellman-ford ...

  5. es6使用箭头函数需要注意的问题

    this问题 箭头函数没有它自己的this值,箭头函数内的this值继承自外围作用域,谁定义的函数,this指向谁 箭头函数要实现类似纯函数的效果,必须剔除外部状态.所以箭头函数不具备普通函数里常见的 ...

  6. Jmeter逻辑控制器,简单操作

    1. 2. 循环控制器可以设置请求的循环次数或永久循环, .  作用:改控制器下的取样器请求可以循环运行. 3. 请求需要拖拽到循环控制器里, 4.循环次数乘以线程数 得到如下图: 成功了 二. 事务 ...

  7. 微信小程序爬坑记

    1.this.setData修改数组里的值1).data: { hide:[true,true] },this.setData({ 'hide[0]': false});2).var str = &q ...

  8. CSS -- 盒子模型之边框、内边距、外边距

    一.使用border为盒子添加边框 盒子模型的边框就是围绕着内容及补白的线,这条线你可以设置它的粗细.样式和颜色(边框三个属性). 1.border-style(边框样式)常见样式有: dashed( ...

  9. Azure Storage 系列(五)通过Azure.Cosmos.Table 类库在.Net 上使用 Table Storage

    一,引言 上一篇文章我们在.NET 项目中添加了 “WindowsAzure.Storage” 的 NuGet 包进行操作Table 数据,但是使用的 “WindowsAzure.Storage”  ...

  10. 设计模式之Command

    由于学习hystrix的使用和原理   所以就学习了command模式https://www.jdon.com/designpatterns/command.htm Command模式是最让我疑惑的一 ...