1

I want to send different JSONs to an endpoint:

{{URL_API}}/products/{sku}

I need to update several information related to different products so i need to specify the product within the endpoint, i mean, i.e:

If you access this particular endpoint: {{URL_API}}/products/ you will get all the products but i need to specify the product that i want to update:

{{URL_API}}/products/99RE345GT

Take a look at this, i want to send a JSON like this:

{
    "sku": "99RE345GT",
    "price": "56665.0000",
    "status": 1,
    "group_prices": [
        {
            "group": "CLASS A",
            "price": 145198.794
        },
        {
            "group": "CLASS B",
            "price": 145198.794
        },
        {
            "group": "CLASS C",
            "price": 145198.794
        }
    ]
}

AND another one like this (both JSONs share the same structure BUT with different information):

{
    "sku": "98PA345GT",
    "price": "17534.0000",
    "status": 1,
    "group_prices": [
        {
            "group": "CLASS A",
            "price": 145198.794
        },
        {
            "group": "CLASS B",
            "price": 145198.794
        },
        {
            "group": "CLASS C",
            "price": 145198.794
        }
    ]
}

How can i do that?.I have already generated more than 200 JSONs for every product..

So, i have to update 200 products so i generated one JSON for every product, do you get me?

Following my example i would need to edit (somehow) the endpoint for every product and send a JSON, i.e:

since the first JSON has the SKU: 99RE345GT it should perform a http method: PUT over this enpoint:

{{URL_API}}/products/99RE345GT

Then, since the second JSON has the SKU: 98PA345GT it should perform a http method: PUT over this enpoint:

{{URL_API}}/products/98PA345GT

I have never done something like this before.. i read something about CSV + POSTMAN runner but i did not understand the way.

EDIT I was working on a file (Excel file) and i did this:

image

image2

So now i have all the different JSON for every product.

EDIT#2. It fails when it validates de Request_URL

I did this: 1)I created a new collection

image

2)I put this Request_url: {{URL_API}}/products/{{sku}}

3)I saved the changes and then, i went to the Collector Runner:

image

4)After cliking on the run button. i got this error message:

Invalid URL:

IMAGE

JustToKnow
  • 785
  • 6
  • 23

1 Answers1

1

Have you tried adding those data sets to a CSV?

https://learning.postman.com/docs/postman/collection-runs/working-with-data-files/

If you have 2 column headers in a CSV file, one with sku and the other with requestBody - Add that variable value to the request body of the PUT request instead of the JSON.

sku,requestBody
99RE345GT, {JSON Payload}
98PA345GT, {...} 

Add a couple of values under those headings to start with, once you prove that it works in the collection Runner.

Once you're happy, add the rest into the file. You may need to do some parsing of the JSON in the Pre-request Script but it should work.

Alternatively, use this template in the PUT request body and this create a CSV withe same column heading as the values in the {{...}} syntax. The values in the datafile will resolve to the values in the request body.

{
    "sku": "{{sku}}",
    "price": "{{price}}",
    "status": {{status}},
    "group_prices": [
        {
            "group": "{{groupA}}",
            "price": {{groupAPrice}} 
        },
        {
            "group": "{{groupB}}",
            "price": {{groupBPrice}}
        },
        {
            "group": "{{groupC}}",
            "price": {{groupCPrice}}
        }
    ]
}

The CSV might look like this:

sku,price,status,groupA,groupAPrice,...
99RE345GT,1234,1,Group A, 555
98PA345GT,1235,1,Group A, 666
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • Thank you for replying, dude.. i'm gonna check that out! – JustToKnow Mar 22 '20 at 15:43
  • I'll update the answer later with a working example but it should be straight forward. Just remember to save the request in the builder as the runner won't know about any changes/updates to the request without that. – Danny Dainton Mar 22 '20 at 15:54
  • Hey pal, thank you :). I was working on a file (Excel file) and now i have 2 columns.. one with the SKUs and other with the JSONs (i have edited the thread). I tried to follow the idea given in this link: https://learning.postman.com/docs/postman/collection-runs/working-with-data-files/ but i failed :(. Could you please help me? What should i do next? Convert the Excel file into CSV?And then? – JustToKnow Mar 22 '20 at 17:58
  • It's should be in a .csv file format as the runner only recognises CSV and JSON files. There should be an example of that format on the learning center link. – Danny Dainton Mar 22 '20 at 18:01
  • Take a look at this image: https://imgur.com/a/70hQ843 I want POSTMAN to "read" my CSV file and automatically update every product using each JSON. I followed the instructions you gave me but i am not getting anything at all :( – JustToKnow Mar 22 '20 at 18:29
  • You would use `{{sku}}` as the variable in the request URL. That references that column in the file. The next column, JSON, as you're calling it would be what you put in the PUT request body as `{{JSON}}`, that will need to be valid JSON though. Open the Collection Runner, select the collection and the datafile. The preview option with show you whats going to get run, if you're happy - Press run – Danny Dainton Mar 22 '20 at 18:40
  • Gonna check this out and i will tell you: P.S: i gave you a +1 because you are helping me a lot :) – JustToKnow Mar 22 '20 at 19:18
  • I will follow the new method you wrote in your answer, are these steps correct? 1)Put this request URL: {{URL_API}}/products/{{sku}} 2)Put a request body and then, make the CSV. A question: should i open the Collection Runner, select the collection and the datafile? Is it the only way? – JustToKnow Mar 22 '20 at 19:35
  • If you have looked at the learning center tutorials, you would see that it's the only way to use a datafile with your Collection, inside the app. – Danny Dainton Mar 22 '20 at 20:09
  • Pal, it throws an error:it says this URL does no exist: {{URL_API}}/products/{{sku}}.. It does not understand the {{sku}}part. Take a look at my thread,i have put different images. What should i do? – JustToKnow Mar 22 '20 at 22:19
  • Where are you setting the URL variable? That console error say to me that your not setting it anywhere that it can read it from. Drop me a message on community.postman.com and I'll take a look in the morning. This is a basic usecase so it's probably something environmental. – Danny Dainton Mar 22 '20 at 22:45
  • The error message is over the {{sku}} inside the request_url.. it throws this error message: unresolved variable, this variable is not defined in the active collection, environment or globals.How can i solve this? :/ – JustToKnow Mar 23 '20 at 02:00
  • The error message is over the {{sku}} inside the request_url.. it throws this error message: unresolved variable, this variable is not defined in the active collection, environment or globals.How can i solve this? :/ – JustToKnow Mar 23 '20 at 02:00
  • The error message text suggests that all the values are not getting resolved, not just the `{{sku}}` value. Ask the question on the https://community.postman.com site, you get an answer quicker and have more freedom to discuss the problem. – Danny Dainton Mar 23 '20 at 08:07
  • Danny, how are you dude? Could you please take a look at this: https://stackoverflow.com/questions/60878237/defining-a-new-variable-in-order-to-make-a-huge-iteration-giving-me-an-error – JustToKnow Mar 27 '20 at 00:34
  • I saw that you asked the question on the Postman community, I'll take a look and reply on there – Danny Dainton Mar 27 '20 at 07:32