cURL post json data, json array and image files REST api testing

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"}]


Sometimes you would want to use shell+curl to make RestAPI Calls, and you may want to pass complex json as data. And in case you want to use variables and create that data during execution time, you probably end up using lot of escape characters () and code would look ugly. I was doing the same thing as well, Windows powershell has a method to convert Dictionary|Associative Array to JSON which really helps in that realm but looked for something similar but couldn't find anything. So this is a sample code that would help:

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

Hope this helps someone.

链接地址: http://www.djcxy.com/p/1336.html

上一篇: 如何在JSON中使用curl发布变量

下一篇: cURL发布json数据,json数组和图像文件REST API测试