Score:0

Google script urlfetchapp provides different results than browser (wordpress REST api), why?

ly flag

I simply need to pull all my users from wordpress. When I urlfetchapp as in the script below, it gives a 406 error but shows the first users in its truncated response, so I set muteHttpExceptions and get the first full page of users. I cannot get a second page or otherwise use queries in the URL.

Here's the kicker, if I disable my authentication, urlfetchtapp gives the same results and same failure to accept queries, but a browser will give me a different set of results when going to the same URL, by providing a list of only the admin users, but IT ACCEPTS QUERIES.

I've tried mimicking the headers the browser sent and that didn't work...

This is a hosted wp deployment (on cPanel) on an apache server.

The major difference in the response headers (beyond the authenticated script's side setting cookies and timeouts), is:

urlfetchapp

Link=https://example.com/wp-json/wp/v2/users?page=2; rel="next" x-wp-total=187 x-wp-totalpages=19

vs browser

Link=https://example.org/wp-json/ X-WP-Total: 8 X-WP-TotalPages: 1

The code I use is simple:

function newTest(){
  var url = "https://example.com/wp-json/wp/v2/users";
  var pw = Utilities.base64Encode('<user>:<app-pw>')
  var options = {
    "method": "post",
    "headers": {
      "Authorization": "Basic "+ pw,
    },
    "contentType": 'application/json',
    "muteHttpExceptions": true
  }
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getHeaders());
  Logger.log(response.getContentText());
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.