Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»Making a POST Request with a JSON Payload using Curl

    Making a POST Request with a JSON Payload using Curl

    By RahulJuly 6, 20172 Mins ReadUpdated:January 1, 2023

    Question- How to send a POST Request with a JSON Body using the Curl Command Line?

    Advertisement

    The `curl` command line utility is a powerful tool for making HTTP requests. It can be used to send a variety of different HTTP requests, including POST requests with a JSON body. Here’s how you can use curl to send a POST request with a JSON body:

    • Create a JSON file
    • Create a JSON file that contains the data you want to send in the request body. For example, let’s say you have a file named `data.json` with the following contents:

      1
      2
      3
      4
      {
        "name": "John Doe",
        "age": 25
      }

    • Curl POST Data
    • Use the curl command to send a POST request with the JSON data. The `-X` option specifies the request method (in this case, POST), and the `-H` option adds an HTTP header (in this case, `Content-Type: application/json` to specify that the request body is in JSON format). The -d option specifies the request body, and the `@` symbol tells curl to read the data from a file.

      Here’s the command to send the POST request with the JSON data:

      curl -X POST -H "Content-Type: application/json" -d @data.json http://example.com/endpoint 
      

    • Curl POST Data with Response Header
    • If the request is successful, the server will return a response. You can use the `-i` option to include the response headers in the output, or the `-o` option to save the response to a file.

      Here’s an example of using the `-i` option to print the response headers:

      curl -X POST -H "Content-Type:application/json" -d @data.json http://example.com/endpoint -i 
      

      And here’s an example of using the `-o` option to save the response to a file:

      curl -X POST -H "Content-Type: application/json" -d @data.json http://example.com/endpoint -o response.txt 
      

    That’s all there is to it! With these simple commands, you can use `curl` to send a POST request with a JSON body to a server.

    Keep in mind that the JSON data in the request body must be properly formatted and valid, or the request may fail. You can use a tool like JSONLint (https://jsonlint.com/) to validate your JSON data before sending it in the request.

    I hope this tutorial has been helpful in showing you how to use `curl` to send a POST request with a JSON body. If you have any questions or need further assistance, don’t hesitate to ask.

    curl json POST
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Sed Command to Delete Lines in a File

    Sed Command to Delete Lines in a File

    Sort Command in Linux with Practical Examples

    15 Practical Examples of Sed Command

    Sed Command in Linux with 15 Practical Examples

    View 5 Comments

    5 Comments

    1. Albert on January 22, 2021 11:02 pm

      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?

      Reply
    2. Kim on May 27, 2020 9:26 am

      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!

      Reply
    3. Vikramsinh Jadhav on October 30, 2018 6:14 pm

      this is very helpful. thanks a lot !!

      Reply
    4. VIJAY on April 20, 2018 2:33 am

      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": "[email protected]",
      "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

      Reply
    5. su on February 15, 2018 12:08 pm

      Hello,

      Thanks for update ideas.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Error: EACCES: permission denied, scandir (Resolved)
    • How To Install Python 3.11 on Ubuntu 22.04 / 20.04
    • How to Install Python 3.11 on Amazon Linux 2
    • An Introduction to the “./configure” Command: Compiling Source Code in Linux
    • How to Install PHP 8.x on Pop!_OS
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.