Cursor Based Paging - No Link Header in The Response

Solved
Highlighted
Excursionist
23 2 6

Hi,

 

I am trying to make a request to paginated REST Admin API endpoints - version 2019-10 or 2019-07 using Google App Scripts.

The endpoint url is: https://mystorename.myshopify.com/admin/api/2019-10/orders.json?limit=2;

I know the total results is more than 200 orders. However, in the response, I see 2 orders but not the header. I can't find the Link header anywhere in the response. I also tried it with 2019-07 version as well, but it did not help.

 

Does anyone experience the same issue, or have any idea what's wrong with it. I definitely read  the documentation multiple times, but still can't get the Link header.

 

Thanks!

0 Likes
Highlighted
Shopify Partner
12 0 1

I tried as well and was wondering why it didn't return any orders.

Then I remembered that I had to specify the status as well since the default is "open":

 

"../api/2019-10/orders.json?limit=2&status=any"

 

Now it returns the link in the header. Maybe you have two open orders and that's why it doesn't return the header?

0 Likes
Highlighted
Excursionist
23 2 6

@29Palms wrote:

I tried as well and was wondering why it didn't return any orders.

Then I remembered that I had to specify the status as well since the default is "open":

 

"../api/2019-10/orders.json?limit=2&status=any"

 

Now it returns the link in the header. Maybe you have two open orders and that's why it doesn't return the header?


Thanks for your reply @29Palms !,

 

I actually have status = any, and also if I change limit to 250, I will get all 250 orders. That means my request returns orders, but not the Link header.

0 Likes
Highlighted
Shopify Partner
12 0 1

Hm then I'm out of ideas query-wise. I have tested the query with the Chrome console, did you try other clients like Postman?

0 Likes
Highlighted
Excursionist
23 2 6

This is an accepted solution.

@29Palms Thanks for taking time looking into this. I have figrured it out.

 

For anyone who might have a problem with Google App Scripts, I could not get the Link header because I converted to JSON before calling response.getHeaders(). To get the header, you need to call response.getHeaders() after fetch the URL to Shopify API. the function response.getHeaders() won't give you anything if you convert the response to JSON.

0 Likes
Highlighted
Tourist
3 0 1

I got the headers just fine but I am not sure how to take the page_info parameter out of the response without using regular expression. How did you get it from the string?

0 Likes
Highlighted
Excursionist
23 2 6

@enanoRed wrote:

I got the headers just fine but I am not sure how to take the page_info parameter out of the response without using regular expression. How did you get it from the string?


Basically you use split string method to make sure you get them right. Each programming language has a different synctax, but here is my code in Google App scripts. You can probably translate it to whatever languate you use:

//get next page to loop through shopify pages
function getNextPageUrl(linkHeader){
    var linkArray = linkHeader.split(',');
    for(var i=linkArray.length-1;i>=0;i--){
        var linkFields = linkArray[i].split('>;');
        if(linkFields[1].indexOf("next")>-1){
            var nexPageUrl = linkFields[0].trim();
            nextPageUrl = nexPageUrl.substring(1, nexPageUrl.length);
            //Logger.log("substring:"+nextPageUrl);
            return nextPageUrl;
        }
    }
    return '';
}

0 Likes
Highlighted
Tourist
3 0 1

Thanks Homiebie. I used regular expression with my Google Apps Script. I just wish they would have made it so that it would have been like another nested JSON layer

1 Like
Highlighted
Shopify Partner
2 0 2

For anyone struggling the with issue while using the request-promise library , you need to specify {resolveWithFullResponse: true} as shown below: 

 

 
const url = `https://${shop}/admin/api/2019-10/products.json`
  const options = {
    method: 'GET',
    uri: url,
    json: true,
    resolveWithFullResponse: true, <----- 
    qs: {
       order: 'inventory_total+desc', 
       limit: 1, 
       fields: 'id,title'
     },
    headers: {
      'X-Shopify-Access-Token': accessToken,
      'content-type': 'application/json'
    }
  }
 
0 Likes
Highlighted
New Member
2 0 0

Why can't they just give you the page_info in the response as a value just pick and use in your next request. No instead you have to do magic tricks to get to the value...

0 Likes