How to POST JSON data with cURL

How to POST JSON data with cURL

If you’re working with APIs, you might have to send JSON data to the server at some point. cURL is a powerful tool that allows you to interact with web services from the command line. In this tutorial, you will learn how to POST JSON data with cURL.

Before you start, make sure you have cURL installed on your system. If you’re on a Linux or macOS machine, it should already be installed. If you’re on a Windows machine, you can download it from the official website.

How to POST JSON data with cURL

By following the steps outlined in this tutorial, you can easily send JSON data with cURL and handle it on the server using your preferred server-side technology.

  • Step 1: Create your JSON data
  • Step 2: Use cURL to send JSON data
  • Step 3: Test the response

Step 1: Create your JSON data

Before you can send JSON data with cURL, you need to create the data want to send. JSON stands for JavaScript Object Notation and is a lightweight data-interchange format. It is easy to read and write for humans and easy to parse for machines. Here’s an example of a simple JSON object:

{
    "name": "John Doe",
    "age": 30,
    "email": "[email protected]"
}

Step 2: Use cURL to send JSON data

Once you have our JSON data, you can use cURL to send it to a server. To do this, need to use the -X POST option to tell cURL that want to make a POST request, and the -d option to specify the data want to send. Here’s an example:

curl -X POST \
     -H "Content-Type: application/json" \
     -d '{"name":"John Doe","age":30,"email":"[email protected]"}' \
     http://example.com/api/users

Let’s break down this command:

  • curl: The cURL command.
  • -X POST: Specifies that we want to send a POST request.
  • -H "Content-Type: application/json": Sets the content type to JSON.
  • -d '{"name":"John Doe","email":"[email protected]","age":30}': Specifies the JSON data we want to send.
  • http://example.com/api/users: The URL we want to send the request to.

Note that the JSON data is enclosed in single quotes (') and that the property names and values are enclosed in double quotes (").

Step 3: Test the response

After you send the request, you should receive a response from the server. And can use cURL to display the response on the command line:

curl http://example.com/api/users

This command will display the response from the server. If everything worked correctly, we should see the JSON data that we sent to the server.

Conclusion

Sending JSON data with cURL is a straightforward process. You can create the JSON data we want to send, use cURL to send the request, and then test the response from the server. By following these steps, you can interact with web services and APIs from the command line.

Recommended Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *