Search results

Make a curl call

Keep current with the latest trends in technical communication by subscribing to the I'd Rather Be Writing newsletter. 5,000+ subscribers

I'm giving an API documentation workshop in Mountain View, California on August 30, 2019. If you're interested, you can register on EventBrite.

In this section, you’ll use curl to make the same weather API requests you made previously with Postman. If you haven’t installed curl, see curl intro and installation first.

Activity: Make the OpenWeatherAPI request using curl

  1. Assuming you completed the exercises in the Postman tutorial, go back into Postman.
  2. On any call you’ve configured, and below the Save button in Postman, click the Code link.
  3. In the Generate Code Snippets dialog box, select cURL from the drop-down list, and then click Copy to Clipboard.

    The Postman code for the OpenWeatherMap weather request in curl looks as follows:

    curl -X GET \
      '' \
      -H 'Postman-Token: dcf3c17f-ef3f-4711-85e1-c2d928e1ea1a' \
      -H 'cache-control: no-cache'

    Postman adds some of its own header information (designated with -H). You can optionally remove these extra header tags (including them won’t hurt anything). You can also remove the backslashes (\), which are just added for readability. Additionally, note that on Windows you must change single quotation marks to double quotation marks because single quotation marks are not supported in the default Windows terminal. (Either approach works on Macs.)

    Here’s the curl call with the -H and backslashes removed, and single quotation marks converted to double quotation marks:

    curl -X GET ""
  4. Curl is available on Mac by default. If you’re on Windows and you haven’t installed curl, follow the instructions for installing curl here. (Note: Choose one of the “free” versions, such as “With Administrator Privileges (free).”)

  5. Go to your terminal. You can open your terminal by doing the following:

    • If you’re on Windows, go to Start and type cmd to open up the command line. (If you’re on Windows 8, see these instructions for accessing the command line.) Right-click and then select Paste to insert the call.

    • If you’re on a Mac, either open iTerm or Terminal (by pressing Cmd + spacebar and typing Terminal). Paste the request you have in your text editor into the command line and then press the Enter key.

    The response from the OpenWeatherMap weather request should look as follows:

    {"coord":{"lon":-121.96,"lat":37.35},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"base":"stations","main":{"temp":65.59,"pressure":1014,"humidity":46,"temp_min":60.8,"temp_max":69.8},"visibility":16093,"wind":{"speed":4.7,"deg":270},"clouds":{"all":20},"dt":1522608960,"sys":{"type":1,"id":479,"message":0.1642,"country":"US","sunrise":1522590719,"sunset":1522636280},"id":420006397,"name":"Santa Clara","cod":200}

    This response is minified. You can un-minify it by going to a site such as JSON pretty print, or on a Mac (with Python installed) you can add | python -m json.tool at the end of your cURL request to minify the JSON in the response (see this Stack Overflow thread for details).

  6. Now make a similar curl request for the 5 day forecast request that you also have in Postman. And another curl request for the third OpenWeatherMap API request you made in Postman.

Note about single and double quotes with Windows curl requests

If you’re using Windows to submit a lot of curl requests, and the curl requests require you to submit JSON in the request body, you might run into issues with single versus double quotes. The problem is that request body content is often formatted in JSON, which requires double quotes. Since you can’t use double quotes inside of other double quotes, you’ll run into issues in submitting curl requests in these scenarios.

Here’s the workaround. If you have to submit body content in JSON, you can store the content in a JSON file. Then you reference the file with an @ symbol, like this:

curl -H "Content-Type: application/json" -H "Authorization: 123" -X POST -d @mypostbody.json

Here curl will look in the existing directory for the mypostbody.json file. (You can also reference the complete path to the JSON file on your machine.)

17% Complete

17/120 pages complete. Only 103 more pages to go.


Want to buy me lunch? Click the Donate button below to donate $10 through Paypal.