How to grab headers before response.json() is called on orders limit request?

Solved
Highlighted
Shopify Partner
28 1 5

I am using react with koa to grab the orders of the store using limits. I want to store the response headers containing the link field for the next request if there are more pages in the request.

 

This is what I am currently trying to get both the data of the request and the headers but this only returns the headers with data undefined.

 

try {
      var headers = '';
      const results = await fetch(`https://${ctx.params.shopOrigin}/admin/api/${API_VERSION}/orders.json?limit=1&created_at_min=2020-03-16&created_at_max=2020-03-18`, orderDataOptions)
      .then((response) => {
        headers = response;
        response.json();
      })
      .then((jsonData) => {
        console.log(jsonData);
        return [headers, jsonData];
      })
      .catch((error) => console.log('error', error));
      let res = await results;
      console.log('res', res);
      ctx.body = {
        status: 'success',
        headers: res[0],
        data: res[1]
      };
    }
    catch {
      ctx.body = {
        status: 'failed to fetch orders.',
    };
    }

 

And this is what I do to grab the data but not able to access the headers as well.

 

 try {
      const results = await fetch(`https://${ctx.params.shopOrigin}/admin/api/${API_VERSION}/orders.json?limit=1&created_at_min=2020-03-16&created_at_max=2020-03-18`, orderDataOptions)
      .then((response) =>  response.json())
        // Once response is turned to json, headers are not available
      .then((jsonData) => {
        console.log(jsonData);
        return  jsonData;
      })
      .catch((error) => console.log('error', error));
      let res = await results;
      console.log('res', res);
      ctx.body = {
        status: 'success',
        // headers: res[0],
        data: res
      };
    }
    catch {
      ctx.body = {
        status: 'failed to fetch orders.',
    };
    }

 

What is the proper way to return the headers in the request while also getting the data from this as well since I seem to only be able to get one or the other? Thanks for the help in advance.

 
0 Likes
Highlighted
Shopify Partner
28 1 5

This is an accepted solution.

Classic rubber ducky scenario :) 

 

This is the solution I was able to come up with to get both.

 

    try {
      const results = await fetch(`https://${ctx.params.shopOrigin}/admin/api/${API_VERSION}/orders.json?limit=1&created_at_min=2020-03-16&created_at_max=2020-03-18`, orderDataOptions)
      .then((response) => {return response})
      .catch((error) => console.log('error', error));
      // header data here
      let res = await results;
      // order data here
      let data = await results.json();
      console.log('data', data);
      var link = '';
      if (res.headers._headers.link[0]){
        link = res.headers._headers.link[0];
      }
      ctx.body = {
        status: 'success',
        link: link,
        data: data
      };
    }
    catch {
      ctx.body = {
        status: 'failed to fetch orders.',
      };
    }

I only return the response with the header info, store it, then grab the json afterward.

 

0 Likes