【问题标题】:cURL post json data, json array and image files REST api testingcURL 发布 json 数据、json 数组和图像文件 REST api 测试
【发布时间】:2015-07-20 16:33:52
【问题描述】:

我遇到了通过 cURL 进行复杂 http 请求的问题。 我正在使用 NODEjs 构建 REST API,使用 Express 路由器和 Multer 中间件来处理多个主体数据和文件。

我的端点路由 127.0.0.1/api/postData 期望: 带有字段的 json 数据,其中一个是 json 对象数组(我有嵌套的猫鼬模式)和 2 个命名图像(png/jpg)。

我需要通过 cURL 发送具有以下 5 对象数据结构的 Post 请求:

name  String
description String
usersArray  Array of json objects like:   [{"id": "123"}, {"id": "456}]
imgIcon  Png/Image    providing  /path/to/imageIcon.png
imgHeader Png/Image     providing /path/to/imageHeader.png

我已经阅读了很多关于 stackoverflow 的帖子,但所有这些帖子都是针对特定单一问题的答案,一个是关于 curl 帖子图像的帖子,另一个是关于 cURL 帖子数组的帖子,但并非全部。

我已经尝试过 POSTMAN 和 DHC(谷歌浏览器)等 REST API 测试工具,除了 arraysArray 字段外,一切都很好 我使用了以下字段:
usersArray[0] {"id": "123"} usersArray[1] {"id": "456"}
但是验证没有通过,因为 json 对象值被解析不正确。

所以我决定将所有内容都放在 cURL 脚本中。

我尝试用以下方式编写我的 cURL 请求:

   #!/bin/bash
   curl -H 'Content-Type: application/json'
  -H 'Accept: application/json'  -X POST 
-F "name=Foo Name Test" 
--data '[{"id": "a667cc8f-42cf-438a-b9d8-7515438a9ac1"}, {"id": "7c7960fb-eeb9-4cbf-9838-bcb6bc9a3878"}]' 
-F "description=Super Bar" 
-F "imgIcon=@/home/username/Pictures/imgIcon.png" 
-F "imgHeader=@/home/username/Pictures/imgHeader.png" http://127.0.0.1:7777/api/postData

当我在 bash 中运行我的 cUrl 脚本时 ./postData

我得到了这个: $ 警告:您只能选择一个 HTTP 请求!

您可以帮助:

1) 知道如何在 cURL 中编写如此复杂的 HTTP REST 请求

2) 或者建议使用工具(如 DHC、POSTMAN)来解决这个复杂的 http 请求。

3) 或知道如何借助 request.js 节点 http 请求库编写此请求。

提前感谢大家的所有答案、想法和想法!

问候,JJ

【问题讨论】:

    标签: arrays json image rest curl


    【解决方案1】:

    您可以尝试使用 POSTMAN 或 google chrome ext 应用程序进行 REST api 测试 DHC。 但是您应该使用多维数组而不是使用 JSON 对象作为值,这可能会导致验证期间出现问题。

    在 DHC 中试试这个:

    |FIELD|            |VALUE|
    name               'Some name'
    description        'Some description'
    usersArray[0][id]  89a7df9
    usersArray[1][id]  dskf28f
    imgIcon            (select type of field "file" and upload image)
    imgHeader          (select type of field "file" and upload image)
    

    上面的工作方式是: usersArray[0][id] 指定多维数组,并在位置 0 上放置一个对象 {},其键为“id”和您在值部分中指定的值。

    所以 usersArray[0][id] "123" 创建 [{"id": 123}] usersArray[1][id] "456" 将另一个元素添加到数组中,因此数组变为: [{"id": "123"},{"id": "456"}]

    【讨论】:

    • 酷,在 DHC 中完美运行!从现在开始,我将使用 DHC 处理这种复杂的 http 请求案例。
    【解决方案2】:

    有时您可能想使用 shell+curl 进行 RestAPI 调用,并且您可能希望将复杂的 json 作为数据传递。如果您想在执行期间使用变量并创建该数据,您可能最终会使用大量转义字符 () 并且代码看起来很难看。我也在做同样的事情,Windows powershell 有一种将 Dictionary|Associative Array 转换为 JSON 的方法,这在该领域确实有帮助,但寻找类似的东西但找不到任何东西。所以这是一个有用的示例代码:

    #!/bin/Bash
    # Author Jayan@localhost.com
    
    ## Variable Declaration
    email="jayan@localhost.com"
    password="VerySecurePassword"
    department="Department X"
    
    ## Create JSON using variables in Bash
    creds="{^email^:^${email}^, ^password^:^${password}^}"
    department="{^department^:^${department}^}"
    authdata_crippled="{^Authenticate^:[${creds},${department}]}"
    # Replace ^ with "
    authdata_json_for_RestAPI_Call=$(echo ${authdata_crippled}|tr '^' '"')
    
    # Testing syntax
    # Get "jq": yum install jq OR https://stedolan.github.io/jq/
    echo ${authdata_json_for_RestAPI_Call} | jq .
    
    # Then you make API call using curl
    # Eg:
    curl http://hostname:port/right/endpoint/for/api/Authenticate -X POST --header "Content-Type:application/json" -d ${authdata_json_for_RestAPI_Call} --cookie-jar cookie.data
    

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-03-26
      • 1970-01-01
      • 2014-03-23
      • 2013-09-09
      • 2016-02-10
      • 2016-02-17
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      相关资源
      最近更新 更多