How to send arrays with GET or POST request in POSTMAN Rest Client
Postman rest client is a chrome extension that is basically a powerful HTTP rest client for testing web services. You can check every request using this client. I have been using this for a long time and it’s really awesome. Most of the developer like to use it for better understanding and boost up his/her development process. It makes it easy to test, develop, and deploy both simple & complex HTTP requests. Today I am talking about how can you send a request with the array. Generally, we can send from a request like a key-value pair. But if we need to send an array then what is the process? There is no array built-in array feature in postman.
The first thing let me clarify that how can we send a post request from an HTML form?
1 2 3 4 5 |
<form method="POST" action="https://192.168.0.1243/dashboard/store"> <input name="username" type="text"> <input name="password" type="password"> <input type="submit" value="Sign in"> </form> |
We can set the key in input names like username, password, and like so on. When we can transform this HTML form a request from Postman rest-client then how it looks like?
We can simply send an array as when we submit a post or get a request from an HTML form.
1 2 3 4 5 6 7 8 |
<form method="POST" action="https://192.168.0.1243/pet-choice/save"> <input name="pet[]" type="text"> <input name="pet[]" type="text"> <input name="pet[]" type="text"> <input name="pet[]" type="text"> <input name="pet[]" type="text"> <input type="submit" value="Sign in"> </form> |
Here this HTML form represents that users can add multiple favorite pet name. So that we need to add a pet array in the key field. That’s the same thing we are doing in postman clients. So after postman transform how is look like?
so this will received in the server looks like
1 2 3 4 5 6 7 |
"pet" => array:5 [ 0 => "Cat" 1 => "Dog" 2 => "Bird" 3 => "Rabbit" 4 => "Hamster" ] |
If you want to send some complex array then also you can easily send it from the postman. If your array looks like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// PHP Format $pet_info_array = array( array('name'=>'rabbit','age'=>'2 years','color'=>'white','height'=>'2 inch','weight'=>'.5Kg'), array('name'=>'cat','age'=>'2.5 years','color'=>'black','height'=>'2 inch','weight'=>'.5Kg') ); // After Printing array it looks like this Array ( [0] => Array ( [name] => rabbit [age] => 2 years [color] => white [height] => 2 inch [weight] => .5Kg ) [1] => Array ( [name] => cat [age] => 2.5 years [color] => black [height] => 2 inch [weight] => .5Kg ) ) |
Then the postman transform is given bellow.
And the response will be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
array:1 [ "pet" => array:2 [ 0 => array:5 [ "name" => "Rabbit" "age" => "2 years" "color" => "White" "height" => "2 inch" "weight" => ".5Kg" ] 1 => array:5 [ "name" => "Cat" "age" => "2.5 years" "color" => "Black" "height" => "2 inch" "weight" => ".5Kg" ] ] ] |
Enjoy your learning as you can.
If you have any further queries in your mind then please feel free to reply to me.
Thank you, good one.
Have a question..how will you get these array values into a variable? these ones name” => “Rabbit”
“age” => “2 years”
“color” => “White”
“height” => “2 inch”
“weight” => “.5Kg”
into a variable or how to split and store in a variable.
Thank you
Thanks for your query.
I don’t know that did you get any solution or not.
By the way
you can json_encode this array and pass it using a regular key value in postman.
like
key : value
pet : {“name”:”Rabbit”,”age”:”2 years”,”color”:”White”,”height”:”2 inch”,”weight”:”.5Kg”}
again you catch this value simple using $_REQUEST[‘pet’] & json_decode($_REQUEST[‘pet’]);
Simple Solution. Hope you understand
Thanks
Thank you! Help me a lot!
Thanks!
Thanks a lot.
It saves my time……
{
“assign”: [
{
“userId”: 0
},{
“userId”: 0
},{
“userId”: 0
}
]
how can i send this from postman
Please check these solutions
SOLUTION 1
SOLUTION 2
HOPE THIS WILL RESOLVE YOUR ISSUE.
THANKS & HAPPY CODING !!!