Before diving into the technicalities, it’s crucial to understand what a POST request is and why a JSON payload is used. A POST request is used to send data to a server to create/update a resource. The data sent to the server is stored in the request body of the HTTP request. JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate.

Advertisement

This article will guide you through the process of making a POST request with a JSON payload using cURL, taking a simple JSON object as an example.

Example JSON

For our example, we will use the following JSON object. You can also write this in a file.


{
  "name": "John Doe",
  "age": 25
}

Step-by-Step Guide to Using cURL for a POST Request

  1. Open Your Command Line Interface:

    First, ensure that cURL is installed on your system. Open your command-line interface (CLI) – Terminal on MacOS/Linux, Command Prompt or PowerShell on Windows.

  2. Crafting the cURL Command:

    The basic syntax for a cURL command making a POST request is as follows:

    
    curl -X POST [URL] -H "Content-Type: application/json" -d [JSON_PAYLOAD]
    
    
    • -X POST specifies that this is a POST request.
    • [URL] is the endpoint you are sending the request to.
    • -H "Content-Type: application/json" sets the header to indicate that the payload is JSON.
    • -d followed by the JSON payload is the data you want to send.
  3. Inserting Your JSON Data:

    Replace [JSON_PAYLOAD] with the JSON object. For our example, the command will look like:

    
    curl -X POST http://example.com/api/data -H "Content-Type: application/json" -d '{"name": "John Doe", "age": 25}'
    
    

    Ensure your JSON data is enclosed in single quotes and the JSON keys and string values are in double quotes.

    You can also load JSON data from file. For example:

    
    curl -X POST http://example.com/api/data -H "Content-Type: application/json" -d @data.json
    
    
  4. Executing the Command:

    Press Enter to execute the command. cURL will send the POST request to the specified URL with the JSON data.

    The server’s response to your POST request will be displayed in the CLI. This usually includes a status code and, potentially, a response body.

Tips and Best Practices

  • Endpoint URL: Ensure you have the correct endpoint URL. If you are testing, you might use endpoints designed for such purposes like http://jsonplaceholder.typicode.com/posts.
  • Data Formatting: Be careful with the JSON formatting. Even a small mistake (like a missing comma or an extra brace) can lead to errors.
  • Debugging: If the request fails, use the -v flag for a verbose output that can help in debugging.
  • Security: When working with sensitive data, make sure you’re using HTTPS to encrypt the data being sent.

Conclusion

Using cURL to make a POST request with a JSON payload is a fundamental skill in web development and API testing. This guide provides a basic understanding and a practical example to get you started. Remember, practice and exploration are key to mastering cURL commands.

Happy Coding!

Share.

5 Comments

  1. how to use “Authentication: XXX”?

    I have a case as bellows:
    curl -u admin:admin123 -X POST -H ‘Content-Type: application/json’ -d “@a.json” “http://abc.net:7180/api/v33/clusters/CDH_5.14/services/hdfs/snapshots/policies/hdfs_snapshot_p3”

    But got 405 error. how to fix it? # grep hdfs_snapshot_p3 cloudera-scm-server.log
    2021-01-22 16:44:50,176 INFO scm-web-1371570:com.cloudera.enterprise.JavaMelodyFacade: Entering HTTP Operation: Method:POST, Path:/v33/clusters/CDH_5.14/services/hdfs/snapshots/policies/hdfs_snapshot_p3
    2021-01-22 16:44:50,183 INFO scm-web-1371570:com.cloudera.enterprise.JavaMelodyFacade: Exiting HTTP Operation: Method:POST, Path:/v33/clusters/CDH_5.14/services/hdfs/snapshots/policies/hdfs_snapshot_p3, Status:405
    I google online, some webpage said not to Allow POST. do not know how to do that. Can anyone help?

  2. Thank you. I’ve been looking for a solution to loading a JSON file into a curl command for days – this has fixed it.
    Thank you again!

  3. Hi Rahul,

    Thanks for this valuable information.

    I need to send the mail to the testers with the Jenkins URL using my notification API. I am using the same method you mentioned above, Can you tell me how to invoke the URL in JSON data in dynamic way, since the uri will change for each build.

    CURL COMAND:
    ==============
    curl -d “@request.json” -H “Content-Type: application/json” -H “Authentication: XXX” -X POST https://<notification API URL/notifications/quick-send

    JSON FILE
    ==========
    {
    "templateId": "",
    "recipientPhoneNumber": "",
    "recipientEmail": "tester.group@XXX.com",
    "recipientChannelId": "",
    "recipientDeviceType": "",
    "subject": "JENKINS BDD TEST",
    "message": "HELLO JENKINS",
    "substitutionData": {},
    "inAppMessage": "",
    "inAppSubstitutionData": {},
    "extras": {},
    "scheduled": "2018-1-28T23:38:21.701Z",
    "archiveAfter": "2018-01-28T23:38:21.701Z",
    "emailReplyTo": ""
    }

    URL of Jenkins:
    ———————
    https://jenkins.eastus.cloudapp.azure.com/view/all/job/PojectName/1/cucumber-html-reports/overview-features.html

Exit mobile version