How do I return the response from an HTTP GET request for use in future steps?

This topic was automatically generated from Slack. You can find the original thread here.

Mitch McAlister : hey there. I’m a bit of a noob at this. I’m trying to do something very basic and easy, but I’m hitting a wall. When I run an HTTP get, I can see the response in the console log, but I’m having trouble returning it for use in subsequent steps. Here’s my code:

var axios = require('axios');

var config = await {
  method: 'get',
  url: 'https://boards-api.greenhouse.io/v1/boards/juvo/jobs',
  headers: { }
};

// log response data
await axios(config)
.then((response) => {
  console.log((response.data));
  console.log((response.status));
  console.log(response.statusText);
  console.log(response.headers);
  console.log(response.config);
})
.catch(function (error) {
  console.log(error);
})

//return data to export for future steps

!!! what do I put here to ensure that the response from the GET will return for future steps???

Dylan Sather (Pipedream) : Hi Mitch, creating a workflow for you now that will provide an example

Mitch McAlister : you are awesome

Dylan Sather (Pipedream) : If you add a new step to your workflow, you’ll see the HTTP / Webhook Request action on the right. I’d recommend using that, since it allows you to pass any method / URL / headers / etc. to it, and will return the response body for use in future steps. Take a look at an example here. Just click on the optional fields (e.g. + headers) below the params I’ve entered to add any other headers / data you’d like.

If you want code-level control, you can re-write what you have to something like this. You’ll see the returned data in the variable steps.nodejs.$return_value, where you can access the body, status, and other data returned from axios .

Let me know if that all makes sense / helps

Dylan Sather (Pipedream) : The only real difference in the two methods is:

• You don’t have to write code in the first, but do in the second
• The first method returns just the response body, but the second returns the whole axios object - the body, status, etc.

Mitch McAlister : thank you - let me give it a shot. you have been very helpful.

Mitch McAlister : that was easier than I thought. I guess I was doing it the hard way :wink:

Dylan Sather (Pipedream) : Haha no worries. There’s definitely a little bit to learn with Pipedream so let me know if you have any other questions

Mitch McAlister : - I’ve got everything pulling from greenhouse properly, and I setup a POST to our CMS (Webflow), but I don’t know how to perform the post even for multiple records (from the greenhouse response). I’m currently only able to create 1 single record.

the way I have my parameters setup, they are calling the step path, which has an item specific path. do you have any tips for how to loop the request for each item in the response object?


return await require("@pipedreamhq/platform").axios(this, {
 method: "post",
 url: `https://api.webflow.com/collections/${params.collection_id}/items`,
 
 headers: {
   Authorization: `Bearer ${auths.webflow.oauth_access_token}`,
   "Content-Type": "application/json",
   "Accept": "application/json",
   "accept-version": "1.0.0"
 },
data: {
    fields: {
      name: params.name,
      location: params.offices,
      url: params.url,
      _archived: params._archived,
      _draft: params._draft, 
    }
 }
})

Dylan Sather (Pipedream) : Yes, take a look at https://docs.pipedream.com/workflows/steps/code/nodejs/http-requests/#send-multiple-http-requests-using-a-loop and let me know if that helps

Dylan Sather (Pipedream) : in case it wasn’t obvious, you can click into the code for any step (even pre-existing actions) and modify it. So I’d recommend modifying this step to accept the records from the Greenhouse response and loop over them in a similar manner

Mitch McAlister : ok. I gave it a go… but I’m running into some issues.
Step 1
steps.trigger

Step 2
steps.getjobs

Step3 - steps.webflow_create_collection_item_1

const axios = require("axios");

//store each response and return them in this array
const responses = []

for (const num of steps.getjobs.$return_value) {
  const resp = await require("@pipedreamhq/platform").axios(this, {
    method: "POST",
    url: 'https://api.webflow.com/collections/${params.collection_id}/items',

     headers: {
   Authorization: `Bearer ${auths.webflow.oauth_access_token}`,
   "Content-Type": "application/json",
   "Accept": "application/json",
   "accept-version": "1.0.0"
     },

    data: {
      num, // Will send the current value of num in the loop

      fields: {
      name: params.name,
      location: params.offices,
      url: params.url,
      _archived: params._archived,
      _draft: params._draft, 
    },
  },
});
  responses.push(resp.data)
}

when I try to run this, I get the following error

TypeErrorsteps.getjobs.$return_value is not iterable
at Object.module.exports (/steps/webflow_create_collection_item_1.js:7:33)

Mitch McAlister : isn’t the steps.getjobs.$return_value supposed to bring out the response from the previous step?

when I do this for a single record without the loop it works, so perhaps I am encapsulating the return in the wrong place?

Dylan Sather (Pipedream) : would you mind sharing your workflow with dylan@pipedream.com and I can take a quick look?

Mitch McAlister : thanks

Dylan Sather (Pipedream) : steps.getjobs.$return_value is an object (just a set of key-value pairs), and you can’t iterate over objects on their own. Did you want to iterate over steps.getjobs.$[return_value.jobs](http://return_value.jobs) instead?

Mitch McAlister : ah. yes. I do

Dylan Sather (Pipedream) : then you can do something like:

for (const job of steps.getjobs.$[return_value.jobs](http://return_value.jobs)) {
  console.log(job.id)
  console.log(job.title)
  // etc.
}

Mitch McAlister : there are 6 jobs that I want to create a post for each one

Dylan Sather (Pipedream) : great. Since each job is itself an object, you can reference its properties like I do in the console.log above. So you can send those in the data block of the axios request in the appropriate fields