Search results

Make a curl call

Last updated: Sep 07, 2020

Download PDF

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

This activity assumes you have curl installed. curl is available on Mac and some Windows 10 versions by default. If you’re on an older Windows machine that doesn’t have curl, see installing curl here for details. (Most likely, choose “With Administrator Privileges (free)” 64-bit version.) Close and restart your Command Prompt after installing curl.

To make a request with curl:

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

    curl request in Postman
    curl request in Postman

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

    curl --location --request GET ''

    (In the above code, replace APIKEY with your actual API key.)

    The --location parameter will prompt curl to submit a new request if the URL is a redirect. The --request parameter specifies the operation for the request.

    (Note that previously, Postman would include its own header information, designated with -H. If you see these parameters, delete them since they cause issues when submitted outside of Postman.)

    In general, the code snippets can be copied and pasted directly into your terminal on a Mac. However, for Windows, you must change the single quotation marks to double quotation marks.

    Also, on Windows, if your curl has any backslashes, (\) remove them and put all content onto the same line. (Backslashes are just added for readability). You can make these adjustments in a text editor before pasting the curl command into the Command Prompt.

  4. Go to your Terminal (Mac) or Command Prompt (Windows).

    You can open your Terminal / Command Prompt by doing the following:

    • If you’re on Windows, go to Start and search for cmd to open up the Command Prompt. Paste in the curl request and then press Enter. (If you can’t paste it in, look for paste options on the right-click menu.)

    • If you’re on a Mac, open Terminal by pressing Cmd + spacebar and typing Terminal. (Or go to Applications > Utilities > Terminal). (You could also use iTerm.) Paste in the curl request and then press Enter.

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

    {"coord":{"lon":-121.95,"lat":37.35},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"stations","main":{"temp":68.34,"pressure":1014,"humidity":73,"temp_min":63,"temp_max":72},"visibility":16093,"wind":{"speed":3.36},"clouds":{"all":40},"dt":1566664878,"sys":{"type":1,"id":5122,"message":0.0106,"country":"US","sunrise":1566653501,"sunset":1566701346},"timezone":-25200,"id":0,"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 if you have Python installed, you can add | python -m json.tool at the end of your cURL request to un-minify the JSON in the response (see this Stack Overflow thread for details).

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.)

About Tom Johnson

Tom Johnson

I'm an API technical writer based in the Seattle area. On this blog, I write about topics related to technical writing and communication — such as software documentation, API documentation, AI, information architecture, content strategy, writing processes, plain language, tech comm careers, and more. Check out my API documentation course if you're looking for more info about documenting APIs. Or see my posts on AI and AI course section for more on the latest in AI and tech comm.

If you're a technical writer and want to keep on top of the latest trends in the tech comm, be sure to subscribe to email updates below. You can also learn more about me or contact me. Finally, note that the opinions I express on my blog are my own points of view, not that of my employer.

18% Complete

18/165 pages complete. Only 147 more pages to go.