In order to start making requests, we need to decide how to make the request. API requests are made via the HTTP methods GET, POST, PUT, DELETE, AND HEAD. In our case, we will use the GET method, since our objective is to simply retrieve data without directly modifying it.
An example GET request we can make to use the Sentiment Analysis API looks like this (replace # with API key):
https://watson-api-explorer.mybluemix.net/alchemy-api/calls/url/URLGetTextSentiment?apikey=#&url=http%3A%2F%2Fwww.theatlantic.com%2Fmagazine%2Farchive%2F2016%2F11%2F
the-case-for-hillary-clinton-and-against-donald-trump%2F501161%2F&outputMode=json&showSourceText=0
Pretty intimidating, right? While the URL may look like esoteric gibberish, it’s actually packed with meaningful information we can use to make calls to the API.
Let’s break it down into its individual components:
Immediately after the endpoint URL, we append a query string containing relevant parameters:
the URL of the source content to be analyzed.
You may be wondering what %3A and %2F mean. URLs can only be sent over the Internet using the ASCII character-set. Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format. URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. In this case, the colon and slashes in the original link http://www.theatlantic.com/magazine/archive/2016/11/the-case-for-hillary-clinton-and-against-donald-trump/501161/ were replaced by a %3A and %2F’s, respectively. This encoding ensures that the URL can be used in the preparation of data with a Content-Type of application/x-www-form-urlencoded, as is often used in the submission of HTML form data in HTTP requests.
Now that we have our source URL and API key in the query string, we have all the required parameters necessary to send a valid GET request.
But wait. What about &outputMode=json and &showSourceText=0 ?
These are optional parameters. Optional parameters can be appended to our GET request, allowing us to further customize what data gets accessed and how. In order to use any of these parameters, we simply add the parameter key-value pair to the GET request as such: &key=value
Optional parameters include:
In our example GET request, we have &outputMode=json and &showSourceText=0. Based off the values of our optional parameters, we now know that we will be receiving a response in JSON format and the source text will not be included in the response