Alexander Kleinhans
Alexander Kleinhans

Reputation: 6248

Pipe curl output after using a multi-line request payload

I have a curl request that accepts JSON as its payload. The JSON payload is a multi-line string. I'm having trouble piping the output of this curl after the EOF.

curl https://foo.bar/v1/baz \
        -H "FOO-BAR: BAZ" \
        -X POST -d @- <<'EOF'
{
    "foo" : "foo_foo",
    "bar": {}
}
EOF

This works, but if I want to pipe the output of this to something, for example python -m json.tool, I have a problem. The following doesn't work:

curl https://foo.bar/v1/baz \
        -H "FOO-BAR: BAZ" \
        -X POST -d @- <<'EOF'
{
    "foo" : "foo_foo",
    "bar": {}
}
EOF | python -m json.tool

Upvotes: 0

Views: 504

Answers (1)

chepner
chepner

Reputation: 531055

The here document doesn't begin until the next (logical) line after the command itself. Like any other redirection operator, <<'EOF' does not need to be the final token on the command line.

curl https://foo.bar/v1/baz \
    -H "FOO-BAR: BAZ" \
    -X POST -d @- <<'EOF'  | python -m json.tool
{
    "foo" : "foo_foo",
    "bar": {}
}
EOF

Upvotes: 3

Related Questions