Sending a formatted table in curl body

930 views Asked by At

I am using newman to run some tests and I am trying to post this results either to discord or slack.

here is the output in my console (I can't control the formatting, it just prints this way...)

┌─────────────────────────┬──────────┬──────────┐
│                         │ executed │   failed │
├─────────────────────────┼──────────┼──────────┤
│              iterations │        2 │        0 │
├─────────────────────────┼──────────┼──────────┤
│                requests │       52 │        0 │
├─────────────────────────┼──────────┼──────────┤
│            test-scripts │       52 │        0 │
├─────────────────────────┼──────────┼──────────┤
│      prerequest-scripts │        0 │        0 │
├─────────────────────────┼──────────┼──────────┤
│              assertions │     1813 │       36 │
├─────────────────────────┴──────────┴──────────┤
│ total run duration: 10.1s                     │
├───────────────────────────────────────────────┤
│ total data received: 146KB (approx)           │
├───────────────────────────────────────────────┤
│ average response time: 157ms                  │
└───────────────────────────────────────────────┘

What I am trying to do:

# call newman
local output=$(newman run mycollection.postman_collection.json)

# output verbose file
echo "$output"

curl -i  -H "Content-Type: application/json; charset=UTF-8"  -X POST -d '{"content":"```'"$output"'```"}'  https://discordapp.com/api/webhooks/blablabla

Although echo "$output" prints the table on the console, I get:

{"code": 50006, "message": "Cannot send an empty message"}

However, if I replace

"content":"This is a string"

It works as expected.

enter image description here

How can I send this output table in curl body

1

There are 1 answers

0
chepner On

The first step would be to simplify the quoting to

... -d "{\"content\": \"$output\"}" ...

The second would be to use jq (or a similar JSON tool) to generate the JSON correctly (meaning, anything in the result of $output would be correctly quoted):

... -d "$(jq --arg data "$output" '{content: $data}')" ...