I'm trying to create a POST request in a .sh file that posts some json stored in a variable. After the POST it should store the response of the post for me to check via echo. I'm doing this in a pipeline that's using curl 7.81.0. When I run the code I get:
Usage: curl [options...] Invalid category provided, here is a list of all categories: auth Different types of authentication methods connection Low level networking operations curl The command line tool itself dns General DNS options file FILE protocol options ftp FTP protocol options http HTTP and HTTPS protocol options imap IMAP protocol options misc Options that don't fit into any other category output Filesystem output pop3 POP3 protocol options post HTTP Post specific options proxy All options related to proxies scp SCP protocol options sftp SFTP protocol options smtp SMTP protocol options ssh SSH protocol options telnet TELNET protocol options tftp TFTP protocol options tls All TLS/SSL related options upload All options for uploads verbose Options related to any kind of command line output of curl
I have a curl Get request that happens earlier in the file that works.
test=$(curl -X GET "https://test.com/test/test" -u username:password | jq '{id, things}' > test.json)
When I run a similar post I get the issue:
{
jq -cn --arg url $url --arg key $key '{things: { stuff: $ARGS.named}}'
} | jq -s add > newTest.json
mergedTest=$(jq -s '.[0] * .[1]' test.json newTest.json)
test2=$(curl -X POST https://test.com/test/test -u username:password -h "Content-Type: application/json" --data $mergedTest)
echo $test2
Any help would be appreciated.
In the end it was a syntax problem. To fix this issue I needed to change the -h to -H. After fixing the initial issue I also had to wrap the data object in double quotes e.g. "$mergedTest". The script then worked as desired.
Docs showing '-H' required syntactically: https://ec.haxx.se/http/post/content-type.html
In regards to the lack of double quotes around my $mergedTest variable.
To quote Charles Duffy "you risk the contents of mergedTest being split on whitespace, having those individidual components expanded as globs, and then being sent to curl as several separate, smaller arguments".
Previously asked question showing why double quotes should be used: I just assigned a variable, but echo $variable shows something else