I am trying to call a REST-Api via Bash script (via curl) - and the behavior differs if the curl-command is invoked via script (rather then calling it directly via shell).
So lets start with the API-call:
curl -s -X POST http://localhost:8080/SOMETOKEN/sendMessage -d text="test 123" -d param=SOMEPARAM
The response will be a json-object like this (a little censored, the text-parameter should be the relevant one though):
{
"ok": true,
"result": {
"message_id": xx,
"from": {
"id": xxx,
"first_name": "xxx",
"username": "xxx"
},
"chat": {
"id": xxx,
"first_name": "xxx",
"type": "private"
},
"date": xxx,
"text": "test 123"
}
}
So when i call the same command from a bash-script:
#!/bin/bash
. settings
MESSAGE="test 123"
COMMAND='curl -s -X POST http://localhost:8080/"'$TOKEN'"/sendMessage -d text='$MESSAGE' -d param='$PARAM
ANSWER="$($COMMAND)"
RESULT=$?
echo $ANSWER | jq .
Executing the script generates something like:
{
"ok": true,
"result": {
"message_id": xx,
"from": {
"id": xxx,
"first_name": "xxx",
"username": "xxx"
},
"chat": {
"id": xxx,
"first_name": "xxx",
"type": "xxx"
},
"date": xxx,
"text": "\"test"
}
}
I checked for any differences, but the command is exactly the same. So I tried some variations with the quotation marks (and doing a manual urlencode on whitespaces by replacing them with %20):
COMMAND="curl -s -X POST http://localhost:8080/$TOKEN/sendMessage -d text=\"$MESSAGE\" -d param=$PARAM"
COMMAND="curl -s -X POST http://localhost:8080/$TOKEN/sendMessage -d text=$MESSAGE -d param=$PARAM"
Well I can't really figure out the issue (I'm quite new to bash scripting though), so maybe anyone can give me a hint?
Here's also some output on the HTTP-Requests, maybe it helps:
From shell directly:
POST /SOMETOKEN/sendMessage HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.47.0
Accept: */*
Content-Length: 29
Content-Type: application/x-www-form-urlencoded
text=test 123¶m=SOMEPARAM
From script
POST /SOMETOKEN/sendMessage HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.47.0
Accept: */*
Content-Length: 25
Content-Type: application/x-www-form-urlencoded
text=test¶m=SOMEPARAM
POST /SOMETOKEN/sendMessage HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.47.0
Accept: */*
Content-Length: 26
Content-Type: application/x-www-form-urlencoded
text="test¶m=SOMEPARAM
Any hints are very welcome! Thanks in advance
Daniel
//edit:
Okay, I haven't found the linked answer (Setting an argument with bash). In case anyone is wondering, these are the 2 solutions how I got it to work:
Manual encoding of the url-parameter whitespaces:
MESSAGE=${MESSAGE%\"} MESSAGE=${MESSAGE#\"} MESSAGE=${MESSAGE/\ /%20} COMMAND='curl -s -X POST http://localhost:8080/'$TOKEN'/sendMessage -d text='$MESSAGE' -d param='$PARAM ANSWER="$($COMMAND)"
Call the command directly - the main problem here is that I can't process the stdout output of curl to process the reponse, otherwise it works just fine.
curl -s -X POST https://localhost:8080/"$TOKEN"/sendMessage -d text="$MESSAGE" -d param="$PARAM"
Thanks for the help!
//edit2:
Alright, finally got it!
ANSWER=`curl -s -X POST https://localhost:8080/"$TOKEN"/sendMessage -d text="$MESSAGE" -d param="$PARAM"`
RESULT=$?
echo $ANSWER | jq .