cURL is frequently used by developers working with REST API’s to send and receive data using JSON notation. This has been a common pattern for years, but it has never been seamless. There have been a number of times when I’ve been trying to get a JSON payload to work against an endpoint for a quick test but I can’t get the quotes correct. Daniel Stenberg, founder and lead developer of cURL is now saying it’s time for a change.
cURL is a command line tool used to transfer data to and from servers. It can be used to download files, upload files, or simply query a server for information. cURL is often used in conjunction with scripts or applications that need to communicate with a server.
A REST API is a web-based tool that allows you to communicate with a server via JSON data transfers. A REST API exposes resources (such as user profiles, files, or comments) that may be accessed using HTTP operations (such as GET, POST, PUT, and DELETE). This lets you to easily send and receive data between a server and a client.
This is a common example of sending a JSON payload with curl:
curl -H "Content-Type: application/json" -d '{"name":"Bruce Wayne","occupation":"Batman"}' https://jobhire.com/
This will send the data contained in the JSON string {“name”:”Bruce Wayne”,”occupation”:”Batman”} to the server at https://jobhire.com/. The simplicity of this example is about as reasonable as the idea that Bruce Wayne would need to post to a job board. He’s rich.
The proposed idea would be to add a new tag –jp, which stands for JSON part. You can add multiple parts to build the body on the same command line. You can see how this composition would work for even complex types like lists or grouping in the examples provided in the newly-updated wiki for this proposed feature.
Even the simple example is uesfule because you can already see how you don’t need to deal with quotes.
Input:
--jp a=b --jp c=d --jp e=2 --jp f=false
Body:
{
"a": "b",
"c": "d",
"e": 2,
"f": false
}
There are already examples for more complex structures, such as lists and maps.
Input:
--jp ":list Monday, Tuesday, Wednesday, Thursday"
Body:
[
"Monday",
"Tuesday",
"Wednesday",
"Thursday"
]
--jp map=europe --jp prime[]=13 --jp prime[]=17 --jp target[x]=-10 --jp target[y]=32
{
"map": "europe",
"prime": [
13,
17
],
"target": {
"x": -10,
"y": 32
}
}
This is going to be a nice addition for cURL. Lack of direct JSON support was never a showstopper for me, but I’m looking forward to the new syntax. I appreciate the fact that Daniel Stenberg is taking it on, particularly since this apparently isn’t a use case he deals with frequently.