Is it possible to send an array with the Postman Chrome extension? - google-chrome

I've been using Postman Chrome extension to test out my API and would like to send an array of IDs via post. Is there a way to send something list this as a parameter in Postman?
{
user_ids: ["1234", "5678"]
}

You need to suffix your variable name with [] like this:
If that doesn't work, try not putting indexes in brackets:
my_array[] value1
my_array[] value2
Note:
If you are using the postman packaged app, you can send an array by selecting raw / json (instead of form-data). Also, make sure to set Content-Type as application/json in Headers tab.
Here is example for raw data {"user_ids": ["123" "233"]}, don't forget the quotes!
If you are using the postman REST client you have to use the method I described above because passing data as raw (json) won't work. There is a bug in the postman REST client (At least I get the bug when I use 0.8.4.6).

For me did not work with array[0], array1, .. or array[], array[], ... .
It works more simply:

If you want an array of dicts, try this:

Here is my solution:
use form-data and edit as below:
Key Value
box[] a
box[n1] b
box[n2][] c
box[n2][] d
and you will get an array like this:
{"box":{"0":"a","n1":"b","n2":["c","d"]}}

It is important to know, that the VALUE box is only allowed to contain a numeral value (no specifiers).
If you want to send e.g. an array of "messages" with Postman, each having a list of key/value pairs, enter e.g. messages[][reason] into the KEY box and the value of reason into the VALUE box:
The server will receive:
{"messages"=>[{"reason"=>"scrolled", "tabid"=>"2"}, {"reason"=>"reload", "tabid"=>"1"}], "endpoint"=>{}}

I also had that problem, and solved it by doing the following:
1 - Going to the request header configuration and added the following:
Accept : application/json, text/plain, */*
Content-Type : application/json;charset=UTF-8
2 - To send the json array, I went to raw json format and set the user_ids to array:
user_ids: ["bbbbbbbbbb","aaaaaaaaaa","987654321","123456789"]

Set Body as raw and form the array as follows:

As mentioned by #pinouchon you can pass it with the help of array index
my_array[0] value
my_array[1] value
In addition to this, to pass list of hashes, you can follow something like:
my_array[0][key1] value1
my_array[0][key2] value2
Example:
To pass param1=[{name:test_name, value:test_value}, {...}]
param1[0][name] test_name
param1[0][value] test_value

Go to Header and select Content-Type = application/json then go to body and select raw and then pass an array.

this worked for me. to pass an array of Item object {ItemID,ColorID,SizeID,Quntity}

in headers set
content-type : application/x-www-form-urlencoded
In body select option
x-www-form-urlencoded
and insert data as json array
user_ids : ["1234", "5678"]

This also works for lists within the object:
Id:37
IdParent:26
Name:Poplet
Values[0].Id:1349
Values[0].Name:SomeName
Values[1].Id:1350
Values[1].Name:AnotherName
the equivalent JSON would be:
{
"Id": 37,
"IdParent": 26,
"Name": "Poplet",
"Values": [
{
"Id": 1349,
"Name": "SomeName"
},
{
"Id": 1350,
"Name": "AnotherName"
}
]
}

{
"data" : [
{
"key1" : "value1",
"key2" : "value2"
},
{
"key01" : "value01",
"key02" : "value02"
},
{
"key10" : "value10",
"key20" : "value20"
}
]
}
You can pass like this.

In form-data you can pass a array like this
and in backend you will fetch it like a
"tags"=>["aaaa", "bbb"]
In my case I've to pass two values in a array so I write it two times

Choose either form-data or urlencoded and use the same key "user_ids". The server should receive it as an array.

In form-data,
key value
user_ids[] 1234
user_ids[] 5678

My back-end is written in Ruby on Rails. This is how I sent the array params using Postman. It worked for me.
UPDATE
I'm using x-www-form-urlencoded. I believe it will work too for form-data.

To send an array using form data there's no need to use brackets.
Just send that specific array using the same name in multiple fields.
Like:
my_array:value_1
my_array:value_2

Although this question has already accepted a solution still that solution has a drawback that is we have to repeat the key (Array name) again and again as one key is accepting only one value. Like this:
Imagine we have 10 values or more, should we repeat the same Array name each time? The programmatic answer is NO. Then we should do the following that is a better approach.
Select the form-data as usual
Type Array name in the Key field
Pass the Array in Value field
Like this:
Now, you should be able to send the Array, but wait, this won't be stored in Database like that in my case with MongoDB. So what you have to do is, use the following piece of code to send it like an Array in the Database:
First, we need to parse it using JSON, like this
let user_ids = JSON.parse(body.user_ids);
Now, you can send user_ids variable to database like an Array
That's All!

I tried all solution here and in other posts, but nothing helped.
The only answer helped me:
Adding [FromBody] attribute before decleration of parameter in function signature:
[Route("MyFunc")]
public string MyFunc([FromBody] string[] obj)

Supposing you have the array of object below,
features: [
{
title: { type: String },
type: { type: String },
},
],
To add the values on the form data on the postman, add it this way
features[title]
features[type]
Check also the image below

Here is something that worked for me
{
"user_ids":["[1234, 5678]"]
}
I believe it depends on how the backend is setup most of the time.

N.B Now we are in 2022 if All of the above solutions didn't, just don't panic. pass array name with is value without a bracket and the add it multiple time, just link how the image below is showing. it should work just fine. If It does work, buy me some coffee, hhh

In my case I need to send array of objects, I sent it like this

Request header configuration and added the following
Accept: application/json
You need to suffix your key variable name with []
like key[0][name]
You can insert it in "bulk-edit" mode
Body section in form-data on right side click Bulk Edit and added the following
items[0][prod_id]:174336
items[0][item_weight]:3.400
items[0][item_qty]:1
items[0][item_selected_melting]:92
items[0][item_remarks]:
items[1][prod_id]:12345

Related

Azure Logic App - Parse JSON with dynamic key/name

just want to know if and how I can parse a HTTP response with a dynamic name in a JSON?
I used the Azure Management API to receive the managed identities (system- and user assigned managed identities) to receive all managed identities.
With a foreach I am iterating the results.
If a resource has a system assigned managed identity and user assigned managed identity, the response looks like this:
{
"principalId": "<principalId1>",
"tenantId": "<tenantId>",
"type": "SystemAssigned, UserAssigned",
"userAssignedIdentities": {
"/subscriptions/<subscriptionId>/resourcegroups/<resourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<userAssignedIdentitiesName>": {
"principalId": "<principalId2>",
"clientId": "<clientId>"
}
}
}
Now, I would like to get the <principalId2>.
Unfortunately, the Name of the object is dynamic related to the scope of the resource /subscriptions/<subscriptionId>/resourcegroups/<resourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<userAssignedIdentitiesName>.
How can I parse the JSON to receive the needed <principalId2>?
For all other responses I can easily use the Data operations Parse JSON with the payload I inserted from the HTTP response.
Is there a way to use a wildcard? Otherwise, could I somehow just select the first object of userAssignedIdentities to receive the needed value?
Ok, this should work for you. This is the flow I tested with ...
Initialise JSON
Your JSON as a string, how you do that in your solution may differ slightly.
Initialize XPath Result
Defined as an Array and the expression is ...
xpath(xml(json(concat('{ root: ', replace(variables('JSON'), 'PrincipalId', 'principalId'), '}'))), '(//principalId)[2]')
Initialize Result
A bit more work again but defined as a String and the expression is ...
array(xpath(xml(base64ToString(variables('XPath Result')[0]?['$content'])), '//text()'))[0]
The end result should be your value ...

how to pass the body to a rest API

I can run the following GET
https://myapi/aaa/?a=270&b=dog&c=animal
But I wonder how to save the parameters to a json body and pass to API. That is, what I call is
https://myapi/aaa/ with body
{
"a": 270,
"b": "dog",
"c": "animal"
}
But the response say "Please provide value for a".
Also suppose parameter a and b have multiple values, how to write the request:
I tried https://myapi/aaa/?a=${270,271}&b=${dog,cat}&c=animal but with no luck.
I think you add an extra "/" after aaa. try this:
https://myapi/aaa?a=270&b=cat&c=animal
If you want to send multiple parameters you can do this:
https://myapi/aaa?a=270&a=271&b=cat&b=dog&c=animal
I would suggest to pass the JSON data in the body as a POST request.But if you still want to pass this as a parameter in URL,you will have to encode your URL like below just for example:-
for ex json is :->{"name":"ABC","id":"1"}
testurl:80/service?data=%7B%22name%22%3A%22ABC%22%2C%22id%22%3A%221%22%7D

Postman - use CSV as input

I am using Postman to test a microservice and I was wondering if you can do something like this.
have a collection with 2 GET request (request1, request2) that have
as one of the headers - userId
have a CSV file with two values for userId: test1, test2
run the collection using the CSV file like this: request1 uses the userId= test1 and request2 uses the parameter userId=test2
I know you can run the collection so that it iterates for each value in the CSV file through each request, but I would like to to map each request to a value in the CSV file. is this possible? If yes, how can you do that?
CSV files are only accepted as a iteration data files, so
is this possible to map each request to a value in the CSV file.
answer is: No, you can't.
You should give us more detailed question, but I feel that this link
will be helpful.
Also you can use JSON as datafile instead of CSV and make such construction:
Firstly, set environment variable "count" to 0.
JSON:
[
{
"UserIdHeadersValue":
[
"firstvalue",
"secondValue",
"thirdValue"
]
}
]
And in Scripts:
pre-request:
var count = parseInt(pm.variables.get("count"));
pm.variables.set("headerValue", data.UserIdHeaderValue[count]);
//you can put now that header value using {{headerValue}};
pm.environment.set("count", count+1);

Modify Request Payload with Charles Proxy

I'm sending a post request with a json object in the body of the request. I'd like to change the value of one field in the json object.
For example:
{ "json_params" :
{
"first_name": "John",
"last_name": "Doe",
"fav_foods":{
"dessert": "icecream",
"pizza": "cheese"
}
}
}
How can I use Charles to change "fav_foods" to "least_fav_foods", for example, or "dessert" to "appetizer"?
I've tried using the Rewrite tool, putting "fav_foods" as the match and "least_fav_foods" as the replace.
Also tried using {"json_params": {"fav_foods": {"dessert": "icecream", "pizza": "cheese"}}} --> {"json_params": {"least_fav_foods": {"dessert": "icecream", "pizza": "cheese"}}} but haven't had luck with either format.
I've tested and verified that I am detecting the right request. I can add a header to the request, but can't change anything in the json payload.
Well, turns out it's far simpler than I was assuming. Charles just does a simple string matching search.
Creating a rewrite rule for type Body with Match Value: "fav_foods" -> Replace Value: "least_fav_foods" did what I wanted.

Property Transfer from a testcase response to a request in different testcase

Testing a rest service in SoapUI, I need to do a property transfer from a testcase response to a request in different test case.
JsonResponse in the first testcase is as follows:
{[{
"items":[{
"id": "1234",
"state": "XYZ",
"type" : "ABCD"
},
{
"id": "12345",
"state": "XYZV",
"type" : "ABCDF"
}
]}
The id(just one) from the response has to be directed to the Json request of the second testcase via property transfer
{
workItemsId: ["1234"],
field: "ABCD"
}
I have tried using items[0].id, but that transfers only the value. I need it as an array in the response. Any help would be deeply appreciated. I am so new to SOAP-UI.
IMO the easy way to do so is using the property expansion. For you case you can use the follow notation approach ${Test step name#Response#JSONPath}.
Supposing you have a first testStep named REST Test Request with the follow response:
{
"items":[
{"id": "1234","state": "XYZ","type" : "ABCD"},
{"id": "12345","state": "XYZV","type" : "ABCDF"}
]
}
And you want to use the first id from items array in a second testStep request and setting it inside array you can use the follow notation:
{
workItemsId: [${REST Test Request#Response#$items[0].id}],
field: "ABCD"
}
UPDATE
If as you comment you want to use the JSONPath expression in a property transfer, add a Property transfer testStep, select as source your request, as property response, as path language JSONPath then put the expression $items[0].id and finally select the property where you want to put the result. Finally your property transfer will looks like:
Hope it helps,
Are you using two test cases for this or just two test steps?
Anyways, rather than transferring the property directly to next request, you can set it as a property on test case and then use that property in next request with property expansion.
So,
create a property "id" on test case.
In property transfer step set the value of this property using items[0].id
In next request use property expansion to populate the value, so json will look like:
{
workItemsId:["${#TestCase#id}"],
field: "ABCD"
}
Here are more details about property expansion : https://www.soapui.org/scripting---properties/property-expansion.html