Make a cURL call

If you haven’t installed cURL, see cURL intro and installation first.

In this section, you’ll use cURL to make the same weather API requests you made previously with Postman.

Prepare the weather request in cURL format

  1. Go back into the Weather API on Mashape.
  2. Copy the cURL request example for the second endpoint (weather) into your text editor:

      curl --get --include '' \
      -H 'X-Mashape-Key: EF3g83pKnzmshgoksF83V6JB6QyTp1cGrrdjsnczTkkYgYrp8p' \
      -H 'Accept: text/plain'
  3. If you’re on Windows, do the following:
    • Change the single quotation marks to double quotation marks
    • Remove the backslashes (\)
    • Add -k after --get as well to work around security certificate issues.

    The request should now look like this:

    curl --get -k --include "" -H "X-Mashape-Key: APIKEY" -H "Accept: text/plain"
  4. Swap in your own API key in place of APIKEY.

    In the instruction in this book, APIKEY will always be used instead of an actual API key. You should replace that part with your own API key.

  5. Customize the lat and lng values to the following:

    • lat=1.3319164
    • lng=103.7231246

Make the request in cURL (Mac)

  1. Open a terminal. To open Terminal, press Cmd + space bar and type Terminal.

    Instead of using Terminal, download and use iTerm instead. It will give you the ability to open multiple tabs, save profiles, and more.

  2. Paste the request you have in your text editor into the command line.

    My request for the Mashape Weather API looks like this:

      curl --get --include '' -H 'X-Mashape-Key: APIKEY' -H 'Accept: text/plain'

    For the Aeris Weather observations endpoint, it looks like this:

      curl --get --include ",ca?client_id=CLIENTID&client_secret=CLIENTSECRET" "Accept: application/json"
  3. Press your Enter key.

    You should see something like this as a response:

    The response should look something like this:

    29 c, Thunderstorms at Singapore, Singapore

Make the request in cURL (Windows 7)

  1. Copy the cURL call from your text editor.
  2. Go to Start and type cmd to open up the commandline. (If you’re on Windows 8, see these instructions for accessing the commandline.)
  3. Right-click and then select Paste to insert the call. My call for the Mashape API looks like this:

      curl --get -k --include "" -H "X-Mashape-Key: APIKEY" -H "Accept: text/plain"

    For the Aeris endpoint, it looks like this:

    curl --get --include ",ca?client_id=CLIENTID&client_secret=CLIENTSECRET" -H "Accept: application/json"

    The response should look something like this:

    29 c, Thunderstorms at Singapore, Singapore

contents[0]=’<div class="randomAd"><div class="bs-callout bs-callout-info bs-callout-ad"> <div class="sponsorMessageTitle">Sponsored message:</div>Over 40,000 API developers use SwaggerHub to design and document APIs with Swagger (OpenAPI). Learn how SwaggerHub can help improve your team’s API documentation workflow. Get started for free.</div></div>’ contents[1]=’<div class="randomAd"><div class="bs-callout bs-callout-success bs-callout-ad"> <div class="sponsorMessageTitle">Sponsored message:</div>SwaggerHub was built by the team behind Swagger to help organizations collaborate on their API design and documentation from one centralized platform. Find out why SwaggerHub is the platform for design and documenting APIs with Swagger. Get started for free.</div></div>’ contents[2]=’<div class="randomAd"><div class="bs-callout bs-callout-warning bs-callout-ad"> <div class="sponsorMessageTitle">Sponsored message:</div>SwaggerHub brings together all the power of the open source Swagger tooling into one integrated platform for designing and documenting APIs with Swagger (OpenAPI). Get started for free.</div></div>’ contents[3]=’<div class="randomAd"><div class="bs-callout bs-callout-important bs-callout-ad"> <div class="sponsorMessageTitle">Sponsored message:</div>SwaggerHub is an integrated API design and documentation platform, built for teams to drive consistency and discipline across the API development workflow. Get started for free.</div></div>’ contents[4]=’<div class="randomAd"><div class="bs-callout bs-callout-danger bs-callout-ad"> <div class="sponsorMessageTitle">Sponsored message:</div>Documenting APIs with Swagger? SwaggerHub lets you generate API documentation that’s securely hosted and fully interactive. Import an existing Swagger definition, or start a new API project from right within your browser, no setup required. Get started for free.</div></div>’

var i=0 //variable used to contain controlled random number var random //while all of array elements haven’t been cycled thru while (i<contents.length){ //generate random num between 0 and arraylength-1 random=Math.floor(Math.random()*contents.length) //if element hasn’t been marked as “selected” if (contents[random]!=”selected”){ document.write(contents[random]) //mark element as selected contents[random]=”selected” break; } } </script> </div>

Single and Double Quotes with Windows cURL requests

Note that if you’re using Windows to submit a lot of cURL requests, you’ll eventually run into issues with single versus double quotes. Some API endpoints (usually for POST methods) require you to submit content in the body of the message request. The body content is formatted in JSON. Since you can’t use double quotes inside of other double quotes, you run into issues in submitting cURL requests.

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, but you can also reference the complete path to the JSON file.

Make cURL requests for each of the weather endpoints

Make a cURL request for each of the weather endpoints for both the Mashape weather endpoints and the Aeris Weather endpoints, similar to how you made the requests in Postman.

Contributing back

If you would like to contribute back and encourage more available content and resources, you can click the Donate button below and contribute any amount.

Get new posts delivered straight to your inbox.

Subscriber count: 4,285