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:
If you want to add an additional parameter you will have to use the
& character to separate your parameters. Like so:
Let’s incorporate this into our code!
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.
Since we want to retrieve more specific results, we should create another parameter.
const additionalParams underneath
queryParams, and assign it
& character at the start of the string is used to separate our parameters. The
= character will join the key
'topics' to a value.
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
wordQuery, declare a
topicQuery, and assign it to the
getSuggestions(), change the value of
endpoint to a concatenated string of
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!