Insert script to run before request then iterate through a parameter like ID


#1

Hi,

I think my thought process is correct in the way I’m trying to achieve this, but I’m not sure how to go about it. I’ve searched a few things on Google but I’m new to Postman and this is going over my head, so I apologize in advance.

I’m working with an API that deals with blog posts. In this process, I’d like to “GET” a blog post, run a script that looks at the description value and splits the string into the first 300 characters and assigning that to a new variable, then “PUT” (update) the blog post using this new variable.

In this example, I’ve blocked out any sensitive information and used X’s instead.

1. GET request

https://api.bigcommerce.com/stores/xxxxxxxxxx/v2/blog/posts/{id}

This should return a JSON object with the blog post data. Something like this (for example):

{
  "id": 1,
  "title": "Welcome to BigCommerce",
  "url": "/blog/welcome-bigcommerce/",
  "body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lacinia ex id ullamcorper suscipit. Phasellus sed augue aliquet, dictum risus in, dictum purus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus diam orci, finibus vel arcu id, tempus lobortis sapien. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lacinia ex id ullamcorper suscipit.", 
  "is_published": true,
  "meta_description": "Blank meta desc"
}

2. Script

Take the JSON, build out another object using the same information (in this case we only want to deal with the body and meta description values.

let metadesc = obj.body.substring(0,300);

3. Create JSON object for body of next request

Somehow translate the metadesc variable into the body of the request

{
"meta_description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lacinia ex id ullamcorper suscipit. Phasellus sed augue aliquet, dictum risus in, dictum purus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus diam orci, finibus vel arcu id, tempus lobort"
}

4. Run PUT request and iterate through the IDs (we have 500 blog posts)

https://api.bigcommerce.com/stores/xxxxxxxxxx/v2/blog/posts/{id}

Sorry for this long-winded question. The end-goal is to scrape our store for 500 blog posts, grab them and take the first 300 characters of the description and insert it into the meta desc field. Rather than do this by hand, I know I can use Postman to do this. I have an idea of how to go about this task but not sure how to combine these steps, where to put the script, or how to iterate the PUT request.

Thanks!


#2

A few general concepts you should know first:

Scripts: You can use the Pre-request and Test scripts tabs to add scripts. Pre-request scripts will execute before your primary request. And Test scripts will execute after your primary request.

Variables: You can save data to an environment or global variable. In a test script, you can access the response object like let responseData = pm.response.json();. You can set / get your variable using syntax like pm.environment.set("id", thisParticularIdValue); and pm.environment.get("id");in either the pre-request and test scripts.

Control workflow: You can use postman.setNextRequest("nameOfRequest");to control your workflow and indicate which request to call next, and under which circumstances.

Run the collection: You will execute your requests by running the collection, for example with the collection runner.

There’s a few ways to organize your collection of requests, but one example might look something like this:

  1. GET all posts (then save as postsToUpdate)
  2. GET specific post details (then create new object where description informs meta_description)
  3. PUT update post with new object (then if there are more posts to update, call Request 2)

This Link checker tutorial walks through these concepts to complete a similar task. The tutorial includes a sample collection and environment so you can see how you can use the pre-request and test scripts more powerfully.


#3

Hi Joyce, thank you so much for your reply. I will try this out! I think this is exactly what I needed, now I can piece everything together! :slight_smile: