Skip to Content
Learn
Requests I
XHR GET Requests IV

In the previous exercise, you made a GET request to the Datamuse API to find words that rhyme. In this exercise, we will create a request to set a topic and find adjectives that describe the input word using query strings.

A query string contains additional information to be sent with a request. The Datamuse API allows us to retrieve more specific data with query strings attached to the request URL.

A query string is separated from the URL using a ? character. After ?, you can then create a parameter which is a key value pair joined by a =. Examine the example below:

'https://api.datamuse.com/words?key=value'

If you want to add an additional parameter you will have to use the & character to separate your parameters. Like so:

'https://api.datamuse.com/words?key=value&anotherKey=anotherValue'

Let’s incorporate this into our code!

Instructions

1.

Let’s do something else besides finding words that rhyme. Have const queryParams store the value 'rel_jjb='. This will search for words that describe another word.

Run the code. Then, type in a word and click the submit button on the web page.

2.

Since we want to retrieve more specific results, we should create another parameter. Create another const additionalParams underneath queryParams, and assign it '&topics='.

Reminder: the & character at the start of the string is used to separate our parameters. The = character will join the key 'topics' to a value.

3.

Now, if you were wondering why there’s a second input field, that’s exactly what we’re going to hook up now! The word typed in here will be the value portion of our second parameter.

The second parameter will filter the response using the word typed into the second input field. In the next step, we’ll incorporate this parameter in with our query string.

In the code block of getSuggestions(), under wordQuery, declare a const topicQuery, and assign it to the value of topicField.

4.

In getSuggestions(), change the value of endpoint to a concatenated string of url, queryParams, wordQuery, additionalParams, and topicQuery.

Run the code. Then enter a word and a topic and click submit.

Our request will have returned a response of adjectives that are related to a topic! Feel free to play around with variables and parameters to get more word suggestions!

Folder Icon

Take this course for free

Already have an account?