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


    《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
  • 相关阅读:
    [JNA系列]Java调用Delphi编写的Dll之Delphi与JAVA基本数据类型对比
    JSON和数据集互相转换单元
    Windows管理多个java版本--解决'has value '1.8',but'1.7' is required'的方法
    Spring+SpringMVC+MyBatis+Maven框架整合
    让你的程序通过XP防火墙
    内存共享【Delphi版】
    自学k8s-安装docker特定版本技巧
    自学k8s-k8s集群环境搭建
    自学k8s-安装过程为下载flannel.yml和镜像文件,而需要设置的代理
    Centos开放指定端口命令
  • 原文地址:https://www.cnblogs.com/sinodragon21/p/5363567.html
Copyright © 2020-2023  润新知