How to Test APIs From the Command Line With HTTPie and cURL

Today in our API Testing Series we'll take a look at two command line tools – HTTPie and curl. Along the way you'll create a couple of API tests running against the Trello APITrack this API, which is a real API with formal authentication tokens. (Trello.com is a project management tool.) You'll need a little setup to follow along with the review and to interact with the Trello API. You'll also need to create an account on Trello.com and generate an application key and a token to use the Trello API. Unlike most of the other tools discussed in our series that involve a user interface (like Postman) or involve code (like Airborne), HTTPie and Curl are both command line tools. You can install the latest version of HTTPie at this link. You can install the latest version of curl from this link, or if you're on a Mac or *NIX system, it may already be installed.

When to use

Both of these tools are ideal for exploration. A developer might create a new API endpoint, or edit an existing one and commit her changes to the source code repository. Once the change is live on a server, a tester can access the changes. That tester might have a longer-term goal of performing test automation with a tool like SmartBear Ready API or Postman, but exploration is where things start. From the command line, a tester can make one API call at a time, make observations about the response, and then make more calls based on what she learned. This process helps to discover bugs that wouldn't be found by the sometimes superficial automated tests running in CI, and it also helps technical staff to learn the new API change.

An easy onramp to curl

Go to Google.com, right-click in the white space on the web page, and select View Source. At the command line, type in "curl www.google.com." Note that the response is essentially identical to the source observed after clicking View Source. Curl stands for "c url" or "see url," and implies that text will be retrieved from a web query. Go back to the browser, type in "software," and click the search button. Notice the URL changed to https://www.google.com/#q=software. Try to curl that and you'll get a SSL certificate error. But if you remove the "s" from "https," making the call "http" instead of "https," and you'll get a copy of the page source in your response. That response will look something like this:

Example: curl www.google.com

Calling curl from the command line is as easy as passing in the web page you'd like to download, plus dealing with authentication. Let's talk about how to do that with Trello.

Curl example

Open a new command prompt and enter in the following command:

curl -F idList=YOURIDLISTVALUE "https://api.trello.com/1/cards?key=YOURKEY&token=YOURTOKEN"

YOURIDLISTVALUE in the above snippet refers to a piece of data that is required when POSTing to the Trello API's /cards resource. You can read about that here. Here is a quick breakdown of the command:

  • Curl invokes the curl program
  • The -F parameter means you're sending form data to the server in addition to the name/value pair following the data sent
  • After that is the URL you're POSTing to. You'll probably notice that the URL is in quotes. This is because you're sending two authentication parameters as part of the query string. Curl will reach the ampersand and stop reading at the line without the quotes.

Enter the API call as follows (making sure to substitute your own parameters for idList, key, and token):

curl -F idList=58c6ea575b0858ee3ef5fc56 "https://api.trello.com/1/cards?key=0d2ff6b4e3650f32e39fde0afeabcd3c&token=d18dc9c833b9cb188a0a47be579044e29792dd8b1730716d0a73bbe398f5159c"

Hit Enter and you'll see something that looks like:

Response:
{"id":"59873d4b305d63fe55b90a32","badges":{"votes":0,"viewingMemberVoted":false,"subscribed":false,"fogbugz":"","checkItems":0,"checkItemsChecked":0,"comments":0,"attachments":0,"description":false,"due":null,"dueComplete":false},"checkItemStates":[],"closed":false,"dateLastActivity":"2017-08-06T16:01:15.124Z","desc":"","descData":{"emoji":{}},"due":null,"dueComplete":false,"email":null,"idBoard":"58c6ea575b0858ee3ef5fc55","idChecklists":[],"idLabels":[],"idList":"58c6ea575b0858ee3ef5fc56","idMembers":[]"idShort":39,"idAttachmentCover":null,"labels":[],"manualCoverAttachment":false,"name":"","pos":147455,"shortUrl":"https://trello.com/c/ZJSLYJm5","url":"https://trello.com/c/ZJSLYJm5/39--","stickers":[]}

should be one line and look something like { "idList": "123456abcd" }.

This is the response from the Trello server. You can read more about curl usage in the man page here.

HTTPie example

Create a new file called idList.json. Inside of that file will be a very small bit of JSON containing the idList value for your Trello board. This lets Trello know which board to use for creating your new card.

Justin Rohrman Justin has been a professional software tester in various capacities since 2005. In his current role, Justin is a consulting software tester and writer working with Excelon Development. Outside of work, he is currently serving on the Association For Software Testing Board of Directors as President helping to facilitate and develop various projects such as conferences, the WHOSE skill book, and BBST software testing courses.
 

Comments

Comments(1)