Learn

Since we’ve reviewed the boilerplate code for an async POST request, the next step is to incorporate that logic into making a real request.

In this exercise, we’ll need to retrieve our Rebrandly API key to access the Rebrandly API.

We will then pass in the endpoint and the request object into the fetch() method to make our POST request.

If you reset the exercise at any point, you will have to paste in your API key again at the top!

Instructions

1.

At the top of main.js, assign apiKey to your Rebrandly API key.

2.

The fetch() function should be inside the try{} statement, which should be inside the shortenUrl() function. In the fetch() function, pass in url as the first argument and an empty object as the second argument.

In that empty object you just created, add the following three properties:

  • method with a value of 'POST'
  • body with a value of data
  • headers with a value of the object below:
{ 'Content-type': 'application/json', 'apikey': apiKey }

Run the code. Then paste in the URL again and click the shorten button.

Great job using Rebrandly to shorten your URL!

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?