0

I want to call an API that has following request format:

curl -X POST -H "Content-Type: application/json" \
-H "api-key: API_KEY" \
-d '{"article":
       {"body_markdown":"---\ntitle:A sample article about...\npublished:false\n---\n..."}}' \
<api-url>

I want to create the value of body_markdown param in one .md file. So I create tes.md with following content:

---
title:A sample article about...
published:false
---

content is here

How to copy the content of tes.md file to be a curl command line? I have tried several way but it doesn't work

cat tes.md | curl -X POST -H 'Content-Type: application/json' -H 'api-key: <my-key>' --data '@tes.md' <api-url>

I also tried something like this

curl -X POST -H 'Content-Type: application/json' -H 'api-key: <my-key>' --data '@tes.md' <api-url>

this solution also didn't work.

Where did I do wrong?

chronos14
  • 321
  • 1
  • 5
  • 20

1 Answers1

0

First store the contents of the markdown file in a variable. For sending JSON data you should replace newlines with literal \n characters:

markdown=$(sed 's/$/\\n/' test.md | tr -d '\n')

Then construct the JSON data string:

data_string="{\"article\":{\"body_markdown\":\"$markdown\"}}"

Then send the data with curl:

curl -H "Content-Type: application/json" -H "api-key: API_KEY" -d "$data_string" http://api.example.com
builder-7000
  • 7,131
  • 3
  • 19
  • 43