Curl To Requests



When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it does the following request with a GET if the HTTP response was 301, 302, or 303. If the response code was any other 3xx code, curl re-sends the following request using the same unmodified method.

A reference guide to making GET, POST, PUT, PATCH, and DELETE API calls through the command line via cURL and their Postman equivalents.

Postman is an API testing environment. cURL is a command line tool for transfering data via URLs. When it comes to REST APIs, we can use Postman as a GUI (graphical user interface) and cURL as a CLI (command line interface) to do the same tasks.

Requests

Prerequisites

If you don't yet understand REST or know how to use REST APIs, please read Understanding REST and REST APIs.

  1. To summarize, in the context we discuss here, curl can be useful when you need to make a request from a shell session on another server (I recently used it to test API response latency from VMs in different geographic locations).
  2. The Curl Converter uses the Python Requests Library for the generated code. Test your Curl commands online with the ReqBin Curl Client and convert them to Python code when ready. Curl Converter automatically generates requests library calls for HTTP headers and JSON content.
Curl to json converter

Goals

I'm going to demonstrate how to do GET, POST, PUT, PATCH, and DELETE requests via Postman and cURL. If you don't have Postman, simply download it from the website. cURL should already be installed in your macOS or Linux environment.

Endpoints

I'm going to use JSON Placeholder, an awesome example site for testing API calls. You can follow along and paste all the commands into your terminal to see what response you get.

Here is the map of methods to endpoints we'll be using. /posts means all, and the 1 in /posts/1 represents /posts/{id}, so ID number1.

MethodEndpoint
GEThttps://jsonplaceholder.typicode.com/posts
POSThttps://jsonplaceholder.typicode.com/posts
PUThttps://jsonplaceholder.typicode.com/posts/1
PATCHhttps://jsonplaceholder.typicode.com/posts/1
DELETEhttps://jsonplaceholder.typicode.com/posts/1

You can click those URLs to see the GET values they provide to the browser. You can use the browser for GET, but you'll have to use cURL or Postman to POST, PUT, PATCH or DELETE.

cURL CLI arguments

Here are a few cURL argument we'll pass with our requests. All requests will simply be curl followed by the argument and data to pass.

  • -X --request - Custom request method
  • -d --data - Sends the specified data
  • -H --header - Sends headers
  • -i --include - Display response headers

GET

Curl Request Online

GET retrieves data.

GET Request

You can also use curl -i to get more information from the headers.

Curl Requests In Parallel

All you have to do for Postman is paste the URL, select GET, and send.

POST

POST creates a new resource. It is non-idempotent, meaning that two identical POST requests will create two new resources.

POST Request
POST Request (json)

There are two ways to do this via Postman. After selecting POST, you can go to Body, select x-www-form-urlencoded, and type each individual value in. If you go to Headers, you'll see Content-Type: application/x-www-form-urlencoded.

Or you can go to Body, select raw, select JSON, and send the actual JSON you intend to send. If you go to Headers, you'll see Content-Type: application/json.

PUT

Curl request online

PUT updates an existing resource. It is idempotent, meaning that two identical PUT requests will modify the same resource. A PUT request requires the entire body to be sent through; if any data is missing, that data will be wiped (except automatic values like auto-incrementing IDs and timestamps).

PUT Request

Curl Requests Windows

PUT Request (json)

Sending the values is the same as with POST.

PATCH

PATCH updates an existing resource, and does not require sending the entire body with the request.

PATCH Request
PATCH Request (json)

No change to sending the values.

DELETE

DELETE removes a resource.

Curl requests php
DELETE Request

No values to send.

Authentication

If you need to send additional headers, like Authorization: Bearer or x-jwt-assertion for JWT-based authentication, you can do it through cURL like this.

In Postman, you'll go to Headers and add Authorization as the key and Bearer <JWT_TOKEN> as the value to send authentication values. You can also go to Headers, click Presets, Manage Presets, and put your own reusable variables in for any headers or values you'll be reusing a lot.

Curl

Conclusion

This guide provides all the basics for getting started with testing your APIs, either through Postman's GUI or cURL's CLI, using JSON or urlencoded form data.