关于postman:发现-postman-自动生成接口调用代码的一个问题

5次阅读

共计 1062 个字符,预计需要花费 3 分钟才能阅读完成。

postman 是程序员常常应用的一个接口测试和验证工具。

咱们在 postman 里点击下图按钮,可能在 Code Snippet 里,看到该接口调用对应的不同编程语言里的实现代码。

比方主动生成对应的 jquery 代码:

然而这些主动生成的代码,我发现并不可能间接复制粘贴了就能用。

比方上图我用 postman 调用的接口,应用的 HTTP 办法是 POST.

主动生成的 jQuery 代码,method:POST,乍一看也指定了 HTTP 办法类型为 POST.

但我粘贴到 HTML 代码里一跑,最初的 method 却是 GET,而不是冀望的 POST:

 var settings = {
            "url": "https://aip.baidubce.com/rest/2.0/ocr/v1/taxi_receipt?access_token=335-23882959",
            "method": "POST",
            "timeout": 0,
            "headers": {"Content-Type": "application/x-www-form-urlencoded"},
            "data": {
              "image": "11",
              "name": "2"
            }
          }
          /*
          $.ajax(settings).done(function (response) {console.log(response);
          });*/

通过钻研,上面才是正确的写法,即把 “method”: “POST” 替换成 type: “POST”

$.ajax({
            type: "POST",
            url: settings.url,
            data: settings.data,
            dataType: "application/x-www-form-urlencoded"
          });

我的 jQuery 版本较低:jQuery/jquery1.7.1.js

胜利发送的 HTTP 申请,key 为 image,value 为 11,在 Chrome 开发者工具里显示如下图所示:

form data:

并没有呈现在 url 尾部:

postman 里图片的 base64 编码,是主动 encode 过的:

因而用 ABAP 调用时,咱们必须手动执行这个 encode 动作。

如果用 curl 测试,应用 –data 指定申请体的负载。

curl -i -k ‘https://aip.baidubce.com/rest…【调用鉴权接口获取的 token】’ –data ‘image=【图片 Base64 编码,需 UrlEncode】’ -H ‘Content-Type:application/x-www-form-urlencoded’

更多 Jerry 的原创文章,尽在:” 汪子熙 ”:

正文完
 0