Rebrandly returning 403 error

Exercise: https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/intermediate-javascript-requests/lessons/requests-ii/exercises/fetch-post-requests-v

Hi, I’m working on POST requests and in this exercise, I’m using Rebrandly to shorten URLs, the exercise is not detecting any errors but when the request is sent Rebrandly returns a 403 error.

// Information to reach API
const apiKey = 'myapikey';    //I inserted my key in the exercise
const url = 'https://api.rebrandly.com/v1/account/links';
const shortenUrl = () => {
  const urlToShorten = inputField.value;
  const data = JSON.stringify({destination: urlToShorten});

  fetch(url, {
    method: 'POST',
    headers: {
    'Content-type': 'application/json',
    'apikey': apiKey
    },
    body: data,
  })
  .then(response => {
    if(response.ok){
      return response.json();
    }
    throw new Error('Request failed!')
  }, networkError => {console.log(networkError.message);})
  .then(jsonResponse => {renderResponse(jsonResponse)})
}

Hi there.

The 403 error could be several things. Can you post the actual content of the error message that you’re receiving? It’s likely that this will be considerably more helpful, seeing as we can’t run your code.

(Well, we can run your code… but one potential cause for your 403 error is that Rebrandly, like most web sites, have a rate limiting policy on their API. We would need your apiKey, which you have rightly removed here, to test whether this is the case. Hence, the request for the full error and not just the slightly vague “I have a 403 error!” :slight_smile:)

Oh, I’m sorry about that.

But now it’s logging a 401 error instead of the 403 error :grimacing:

main.js:15 POST https://api.rebrandly.com/v1/account/links 401
shortenUrl @ main.js:15
displayShortUrl @ main.js:38

No problem; you’re not the first person to fall foul of rate limiting while doing one of the API related lessons here. :slight_smile:

HTTP 401 is Unauthorized - can you check that your API key is right? :slight_smile: