Using cURL to Interact with APIs
What is cURL?
cURL is a command-line utility (on Linux or Mac OS X) for interacting with URLs. It is useful for requesting data from APIs, which can then be processed programmatically. It can be invoked like this:
curl -X [REQUEST] -H '[HEADER]:[VALUE]' [URL]
where
- [hd:REQUEST] is the HTTP request type (e.g. GET)
- [hd:HEADER] and [hd:VALUE] are the name of a header (e.g. Authorization) and the content of that header (e.g. bearer 12345678). If you need to specify multiple headers, you may pass as many -H options as you need. (e.g. curl -X GET -H 'Thing:One' -H 'Fish:Red' ...)
- URL is the url which you wish to access.
cURL Options
curl -s
- “Silent mode” (normally, it prints out how many files it downloaded, and how fast)
curl -I
- Display info (headers) only about a URL
- Useful for examining HTTP status codes
curl -X [REQUEST]
- Issue a [hd:REQUEST] method (POST, PUT, DELETE) instead of a GET
curl -H '[HEADER]:[VALUE]'
- Attach a header to the request
- Can be used multiple times to include multiple headers
curl -d “field=value”
- Issue a POST request with this form data (repeat multiple times for multiple fields)