Is it possible to send an array with the Postman Chrome extension?
Asked Answered
C

26

360

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"]
}
Creodont answered 6/10, 2012 at 4:18 Comment(1)
I'm fairly certain the right way to send an array is "in some way that the particular server you are sending it to will receive it correctly". This accounts for the variation in answers below - but it'd all be a bit more useful if everyone (or anyone?) had said what back-end they were using.Cabrera
C
632

You need to suffix your variable name with [] like this:

send_array_param_with_postman

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).

Chiliasm answered 19/5, 2014 at 10:9 Comment(4)
Just in case someone is asking how to add hashes instead of just array, the idea is still the same, just change the indexes to hash name my_array[hashname] value1Sucy
my_array[] value will create a array parameter with the values provided, as key => [value]. my_array[key] value will create a hash, as {key => value}.Enceladus
none of them worked but I figured out just adding the array name works in 2019. [prntscr.com/nqubpi]Smashed
Your 2nd option without index, solved my issue, ThanksDre
R
98

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

Riana answered 17/9, 2014 at 21:3 Comment(5)
By adding multiple keys with same name, the server will receive the last one onlyLenni
please tell me how to send only one element userid[0] in form-data. if i pass userid[]/userid[0] in a key field it taken as a string!Yatzeck
with asp.net core 2.2, it receives all array elements with this solutionBabby
in Django I was able to get all files with: request.FILES.getlist('usersId'), using this methodConcession
Works in DjangoTees
S
76

If you want an array of dicts, try this: enter image description here

Sizeable answered 17/9, 2018 at 11:4 Comment(6)
For me what worked for node.js / mongoose is social_links[0].name notice the additional periodAlyss
In your example what if name is also an array? I tried something like social_links[0]name[0] in Django REST Framework (nested writable model-serializer with many-to-many relations) and it didn't work.Mowry
As long as I add a '.' after the index (e.g. social_liks[0].name) this works for me from Postman using Spring Rest.Shimberg
unfortunately, social_link[0].name doesn't work for me , when I check postman body request it shows "social_link[0].name" = "136" do you've any Idea why ?Ohaus
how to create a custom request for for this in laravel 8 ?Disvalue
For laravel, social_link[0][name] should workWorcestershire
F
60

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"]}}
Ferroconcrete answered 3/4, 2015 at 3:9 Comment(3)
Great! finally I can send a object "loc": { "type": "Point", "coordinates": [ 126.972967, 37.54171 ], } to type in loc[type]: Point, loc[coordinates][] :126..., loc[coordinates][]: 37... on Postman thx!!Clariceclarie
This is very helpful as when you need to send files, the 'raw' format of Postman just won't work. You need to use the form-data format.Preconscious
The only down drawback of this solution is that if you use number as a key, the blank one will be the last key. array[0], array[1], array[], the blank one will be array[2]Omeromero
F
40

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:

enter image description here

The server will receive:

{"messages"=>[{"reason"=>"scrolled", "tabid"=>"2"}, {"reason"=>"reload", "tabid"=>"1"}], "endpoint"=>{}}
Fleshpots answered 5/9, 2018 at 6:47 Comment(1)
This did the trick... but only when I checked form-data option.Shortstop
Z
37

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"]
Zayin answered 20/11, 2014 at 15:29 Comment(3)
This did the trick for me, setting the Accept header was not necessary howeverJemy
This was the answer I needed.Estienne
My API method expects List<string> and this did the job.Majuscule
T
30

Set Body as raw and form the array as follows:

enter image description here

Thoer answered 4/12, 2017 at 22:54 Comment(1)
{ "question" : "What is capital of India", "marks" : 1, "options" : [ "Mumbai", "Pune", "New Delhi", "Jaipur" ], "correct" : "New Delhi" }Suspensor
G
24

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
Gloriole answered 4/7, 2016 at 15:46 Comment(1)
This worked for me. Since, i also needed to send image file in request, along with array of object this is what i needed. There is no way to send the image file in raw format(json)Foley
V
19

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

Video answered 10/12, 2018 at 15:43 Comment(0)
S
17

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

Postman data

Sulfur answered 30/6, 2017 at 14:33 Comment(0)
C
10

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"]
Corinthians answered 4/5, 2017 at 14:37 Comment(2)
Only one that worked for me using form data! Couldn't use raw since I'm sending images tooResponsum
The only one worked for me as well For complete newbies: Press x-www-form-urlencoded => bulk edit => UserNames:["UserName1","UserName2", "UserName3"]Crofoot
K
10

In form-data you can pass a array like this

enter image description here

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

Komarek answered 10/3, 2022 at 11:19 Comment(0)
U
9

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"
        }
    ]
}
Untidy answered 25/10, 2017 at 12:6 Comment(1)
unfortunately, Values[0].Id doesn't work for me , when I check postman body request it shows "Values[0].Id" = "1349" do you've any Idea why ?Ohaus
D
9
{
    "data" : [  
        {
            "key1" : "value1",
            "key2" : "value2"   
        },
        {
            "key01" : "value01",
            "key02" : "value02"             
        },
        {
            "key10" : "value10",
            "key20" : "value20"   
        }
    ]
}

You can pass like this.

Dissemblance answered 13/7, 2018 at 13:12 Comment(0)
B
6

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

Bamberg answered 6/10, 2012 at 20:21 Comment(2)
Looks like, due to a bug in Chrome that erroneously sends with an XML header even when you select JSON in Postman, you have to add a Content-type header with value application/json.Candlemaker
just in case someone comes back looking for an answer, the key for an array should be user_ids[] instead of just user_idsEggshell
R
6

Request header configuration and added the following

Accept: application/json

You need to suffix your key variable name with []

like key[0][name]

enter image description here

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
Resonance answered 26/8, 2022 at 7:20 Comment(0)
M
5

In form-data,

   key              value

 user_ids[]         1234
 user_ids[]         5678
Myrwyn answered 16/1, 2018 at 10:25 Comment(0)
W
5

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.

enter image description here

Warbeck answered 25/8, 2021 at 2:51 Comment(0)
S
2

In my case I need to send array of objects, I sent it like this enter image description here

Sivia answered 26/7, 2022 at 12:34 Comment(0)
A
1

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
Anorexia answered 26/4, 2022 at 12:52 Comment(1)
Please double check your understanding of the difference of "answer" and "question". ;-) Harmless mistake of course.Yaker
C
1

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:

Postman form-data (Key-Value Pairs)

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.

  1. Select the form-data as usual
  2. Type Array name in the Key field
  3. Pass the Array in Value field

Like this:

Passing Array in Postman

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:

  1. First, we need to parse it using JSON, like this

let user_ids = JSON.parse(body.user_ids);

  1. Now, you can send user_ids variable to database like an Array

That's All!

Chiao answered 29/8, 2022 at 9:39 Comment(0)
I
0

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)
Inconsecutive answered 9/1, 2017 at 9:16 Comment(0)
P
0

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

enter image description here

Pussyfoot answered 15/1, 2022 at 9:1 Comment(0)
S
0

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.

Scarabaeoid answered 27/1, 2022 at 14:58 Comment(0)
S
0

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

enter image description here

Shunt answered 26/2, 2022 at 13:21 Comment(3)
It will not work. It will fetch the tag2 valueKomarek
@manishnautiyal have you tried it? because that is the way I only use to pass array data and it works as expectedShunt
Yes, I try and it didn't work for me so I wrote.Komarek
G
0

Here is a simple way to do it!

Form-data request in postman

Add the array you want to add above, and then on the backend side Simply Parse it, In my case, since I want an array of User Ref, I have typecast it into The object of user.

Parsing the data

Gilbertine answered 12/3 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.