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

**URL:** <https://community.postman.com/t/insert-script-to-run-before-request-then-iterate-through-a-parameter-like-id/3492>\
**Category:** Help Hub\
**Created:** [December 5, 2018, 2:31pm UTC](https://community.postman.com/t/insert-script-to-run-before-request-then-iterate-through-a-parameter-like-id/3492 "2018-12-05T14:31:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![asiejna](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@asiejna](https://community.postman.com/u/asiejna)\
**Post date:** [December 5, 2018, 2:31pm UTC](https://community.postman.com/t/insert-script-to-run-before-request-then-iterate-through-a-parameter-like-id/3492/1 "2018-12-05T14:31:52Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [December 7, 2018, 7:55pm UTC](https://community.postman.com/t/insert-script-to-run-before-request-then-iterate-through-a-parameter-like-id/3492/2 "2018-12-07T19:55:08Z")

</div>

A few general concepts you should know first:

**Scripts** : You can use the Pre-request and Test scripts tabs to add [scripts](https://learning.getpostman.com/docs/postman/scripts/intro_to_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](https://learning.getpostman.com/docs/postman/environments_and_globals/intro_to_environments_and_globals). 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](https://learning.getpostman.com/docs/postman/scripts/branching_and_looping/) and indicate which request to call next, and under which circumstances.

**Run the collection** : You will execute your requests by [running the collection](https://learning.getpostman.com/docs/postman/collection_runs/intro_to_collection_runs), 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](http://blog.getpostman.com/2017/06/23/check-for-broken-links-on-your-website-using-a-postman-collection/) 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.

---

<div class="post-metadata">

**Author:** ![asiejna](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@asiejna](https://community.postman.com/u/asiejna)\
**Post date:** [December 10, 2018, 1:26pm UTC](https://community.postman.com/t/insert-script-to-run-before-request-then-iterate-through-a-parameter-like-id/3492/3 "2018-12-10T13:26:26Z")

</div>

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! 🙂
