0

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&param=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&param=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&param=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:

  1. 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)"
    
  2. 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 .
Community
  • 1
  • 1
Daniel
  • 1
  • 2
  • Spaces in `$MESSAGE` will be a problem. And you're adding double quotes around `$TOKEN` for some reason. – Barmar Nov 19 '16 at 00:50
  • Hello,thanks for the quick reply. The double quotes around `$TOKEN` are actually me being stupid when copy and pasting the script snippets together. My bad... What do you mean by spaces are a problem within `$MESSAGE` . The part i really don't understand is, that `curl -s -X POST http://localhost:8080/SOMETOKEN/sendMessage -d text="test 123" -d param=PARAM` is the content of the `$COMMAND` variable right before it's called... this looks correct to me? What am I missing? – Daniel Nov 19 '16 at 01:07
  • Quotes are not processed in the expansion of a variable. So you can't put quotes in `$COMMAND` and expect them to make `text="test 123"` into a single argument. See the question I linked to for more on this issue and how to get around it. – Barmar Nov 20 '16 at 21:57

0 Answers0