How to Send Request Body in Post Curl

I have written an API in java. Trying to hit using curl from the shell script.

local CONTENT_SOURCE="$1"

curl "$APP_URL/api/admin/sync" \
  --header 'Content-Type: application/json' \
  --data '{"content": "$CONTENT_SOURCE", "usernames": [""]}' \
  -w "\n"

How to send CONTENT_SOURCE in the request body?

1 Answer

Build your JSON data with jq like this:

JSON_DATA=$(jq --null-input --arg content "${CONTENT_SOURCE}" '{"content": $content, "usernames": [""]}')

And use it with your cURL command:

curl "$APP_URL/api/admin/sync" \
  --header 'Content-Type: application/json' \
  --data "${JSON_DATA}" \
  -w "\n"
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest