卷曲张贴JSON数据,JSON数组和图像文件REST API测试 [英] cURL post json data, json array and image files REST api testing

查看:385
本文介绍了卷曲张贴JSON数据,JSON数组和图像文件REST API测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临通过卷曲复杂的http请求的问题。
我建设REST API的NodeJS与使用防爆preSS路由器和Multer中间件处理多个主体数据和文件。

我的终点路线127.0.0.1/api/postData
预计:
 同场JSON数据,其中之一是JSON对象(我有嵌套的猫鼬模式)和2名为图像(PNG / JPG)的阵列。

我需要通过卷曲发送请求后与以下5个对象的数据结构:

 名称字符串
描述字符串
JSON对象的数组usersArray像:[{ID:123},{ID:456}]
imgIcon PNG /图片提供/path/to/imageIcon.png
imgHeader PNG /图片提供/path/to/imageHeader.png

我已经读了很多关于计算器的线程,但所有的人都回答特定问题奇异,一个线程约卷曲后的图像,另一卷曲后阵列,但不完全。

我试过REST API测试工具,如邮递员,DHC(谷歌浏览器),并有一切都很好,除了arraysArray场
我使用的字段,如:结果
 usersArray [0] {ID:123}
 usersArray [1] {ID:456}结果
但是,验证未通过辩论,因为JSON对象值将被解析不正确莫名其妙。

所以我决定把一切都放在卷曲脚本。

我试着写我在下面的方式卷曲的要求:

 #!/斌/庆典
   卷曲-H内容类型:应用程序/ JSON
  -H接受:应用/ JSON-X POST
-FNAME =富姓名测试
--data[{ID:a667cc8f-42cf-438A-b9d8-7515438a9ac1},{ID:7c7960fb-eeb9-4cbf-9838-bcb6bc9a3878}]
-F描述=超级律师
-FimgIcon=@/home/username/Pictures/imgIcon.png
-FimgHeader=@/home/username/Pictures/imgHeader.pnghttp://127.0.0.1:7777/api/postData

当我在bash运行我的脚本cUrl作者
./postData

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

您可以帮助:

1)不知道如何写卷曲这样一个复杂的HTTP REST请求

2)或工具的建议(如DHC,邮差)来解决这个复杂的HTTP请求。

3)或任何想法如何编写一个带request.js节点HTTP请求库的帮助这一请求。

感谢大家提前为所有的答案,想法和创意!

问候,JJ


解决方案

您可以尝试邮差或Google Chrome分机应用的REST API测试DHC。
但是,你应该使用,而不是使用JSON对象作为一种价值,可以验证过程中导致问题多维数组。

试试这个在DHC:

  |字段| |值|
命名有些名称'
描述一些描述
usersArray [0] [ID] 89a7df9
usersArray [1] [ID] dskf28f
imgIcon(场选择文件的类型和上传图片)
imgHeader(场选择文件的类型和上传图片)

它的工作原理上面的方法是:
usersArray [0] [ID]指定位置0的对象{}与您的价值部分sepcify键ID和值多维数组和场所。

所以usersArray [0] [ID]123创建[{ID:123}]
usersArray [1] [ID]456又增加了元素的数组,所以数组变为:[{ID:123},{ID:456}]

I'm facing a problem with complex http request via cURL. I'm building REST API with NODEjs, using Express routers and Multer middleware to handle multiple body data and files.

My endpoint route 127.0.0.1/api/postData expects: json data with fields, one of which is array of json objects (I'm having nested mongoose schema) and 2 named images (png/jpg).

I need to send Post request via cURL with the following 5-object data structure:

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

I've read a lot of threads on stackoverflow, but all of them are answers to a particular singular problem, one thread about curl post images, another cURL post arrays, but not altogether.

I've tried REST API test tools like POSTMAN and DHC (google chrome), and there everything's fine except for arraysArray field I used the fields like:
usersArray[0] {"id": "123"} usersArray[1] {"id": "456"}
But validation didn't pass, cuz the json object value is parsed somehow incorrectly.

So I decided to put everything in cURL script.

I tried to write my cURL request in following way:

   #!/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

When I run my cUrl script in bash ./postData

I got this: $ Warning: You can only select one HTTP request!

You can help with:

1) Any Idea how to write such a complex HTTP REST request in cURL

2) Or with suggestion of tools (like DHC, POSTMAN) to solve this complex http request.

3) or with any idea how to write this request with the help of request.js node http request library.

Thank you all in advance for all answers, thoughts and ideas!!

Regards, JJ

解决方案

You can try POSTMAN or google chrome ext app for REST api testing DHC. But you should use multidimensional array instead of using JSON objects as a value, that can cause problem during validation.

Try this in 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)

The way it works above is: usersArray[0][id] specifies multidimensional array and places on position 0 an object {} with key "id" and value which you sepcify in value part.

So usersArray[0][id] "123" creates [{"id": 123}] usersArray[1][id] "456" adds another element to array, so array becomes: [{"id": "123"},{"id": "456"}]

这篇关于卷曲张贴JSON数据,JSON数组和图像文件REST API测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆