Logo AppDev24 Login / Sign Up
Sign Up
Have Login?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Login
New Account?
Recovery
Go to Login
By continuing you indicate that you agree to Terms of Service and Privacy Policy of the site.
Shell Script

Safely Posting JSON Data with cURL

 
Updated on Jun 22, 2024

As a developer, we often need to interact with APIs to perform various tasks. One common task is posting JSON data to an API endpoint. In this article, we'll explore how to do just that using the popular command-line tool, cURL.

Posting JSON Data as Payload

When posting JSON data as payload, we can use the following commands in our terminal:

terraform_api='https://app.terraform.io/api/v2'
url=${terraform_api}/runs
helper_tf_apply_destroy='your-terraform-api-token'

response=`curl -s -X POST -H "Authorization: Bearer ${helper_tf_apply_destroy}" -H "Content-Type: application/vnd.api+json" ${url} --data '{"comment":"Confirm Apply"}'`

In this example, we're posting a JSON payload with the comment "Confirm Apply". You can modify the --data option to include your own JSON data.

Posting JSON File as Payload

Sometimes, we may need to post a larger JSON file as payload. In that case, we can use the following commands:

terraform_api='https://app.terraform.io/api/v2'
url=${terraform_api}/runs
helper_tf_apply_destroy='your-terraform-api-token'
dir=$(pwd)

response=`curl -s -X POST -H "Authorization: Bearer ${helper_tf_apply_destroy}" -H "Content-Type: application/vnd.api+json" ${url} --data @${dir}/payload.json`

In this example, we're posting the contents of a file named payload.json located in the current working directory. Make sure to modify the --data option to include the correct path and filename.

Customizing Header

When posting JSON data, we may need to customize our header authentication and content type. For example:

-H "Content-Type: application/json"

This sets the content type of the request to application/json, which is suitable for most JSON-based APIs.

In conclusion, posting JSON data with cURL is a straightforward process that can be achieved using the commands outlined above. By modifying your header and payload as needed, you'll be able to successfully interact with your API endpoints.

PrimeChess

PrimeChess.org

PrimeChess.org makes elite chess training accessible and affordable for everyone. For the past 6 years, we have offered free chess camps for kids in Singapore and India, and during that time, we also observed many average-rated coaches charging far too much for their services.

To change that, we assembled a team of top-rated coaches including International Masters (IM) or coaches with multiple IM or GM norms, to provide online classes starting from $50 per month (8 classes each month + 4 tournaments)

This affordability is only possible if we get more students. This is why it will be very helpful if you could please pass-on this message to others.

Exclucively For Indian Residents: 
Basic - ₹1500
Intermediate- ₹2000
Advanced - ₹2500

Top 10 Articles