2

I'm working on a simple enough website and I need to perform a get request to Imgur's API. Using the following works fine:

curl -v --location --request GET 'https://api.imgur.com/3/album/WFWR56Z/images' \
--header 'Authorization: Client-ID {{clientId}}'

As does the following Python 3 code:

import requests

url = "https://api.imgur.com/3/album/WFWR56Z/images"
headers = {
  'Authorization': 'Client-ID {{clientId}}'
}

response = requests.request("GET", url, headers=headers, data=payload)

Setting up a simple example and serving it on local host using Apache and fetch also works:

    var myHeaders = new Headers();
    myHeaders.append("Authorization", "Client-ID {{clientId}}");

    var requestOptions = {
      method: 'GET',
      headers: myHeaders,
      redirect: 'follow'
    };

    fetch("https://api.imgur.com/3/album/WFWR56Z/images\n", requestOptions)
      .then(response => response.text())
      .then(result => console.log(result))
      .catch(error => console.log('error', error));

However, when I use Angular's HttpClient like so:


  constructor(private http: HttpClient) { }

  getImageLinks(): void {
    const headers = new HttpHeaders(
      {
        'Authorization': "Client-ID {{clientId}}",
      });
    this.http.get("https://api.imgur.com/3/album/WFWR56Z/images", { headers }).subscribe(data => {
      console.log(data);
    })
  }
}

I almost always get a 429 rate limit error. There are rare times when this doesn't occur, but I can't seem to identify when and why it manages to return.

Doogyb
  • 63
  • 5
  • 2
    Angular http calls are done via the browser, which adds its own headers and stuff. Check the network tab in the dev tools, maybe copy the request as curl, and see where that leads you. – Clashsoft Mar 15 '21 at 17:27
  • Thanks @Clashsoft that did the trick, I've answered below. – Doogyb Mar 16 '21 at 09:16

1 Answers1

3

Thanks to @Clashsoft, I downloaded the query as a cURL command and deleted the headers one by one to find the offending header:

-H 'referer: http://localhost:4200/'

Which I fixed using this: Remove http referer

Specifically, I added <meta name="referrer" content="no-referrer" /> to my index.html.

Doogyb
  • 63
  • 5