1

I am passing dynamic value to testing method and executing the curl request. There is an issue with $PARAMETERS.

When I execute the following method, I get error as below

Error:-

curl: option -F: requires parameter
curl: try 'curl --help' or 'curl --manual' for more information

Function:-

testing() {

local URL=$1
local HTTP_TYPE=$2
local PARAMETERS=$3

# store the whole response with the status at the and
HTTP_RESPONSE=$(curl -w "HTTPSTATUS:%{http_code}" -X $HTTP_TYPE $URL $PARAMETERS)

echo $HTTP_RESPONSE
}

URL='https://google.com'
HTTP_TYPE='POST'
PARAMETERS="-F 'name=test' -F 'query=testing'"


result=$(testing $URL $HTTP_TYPE $PARAMETERS)

FYI, above is a sample method and am using shell script. Kindly tell me how to solve this?

Galet
  • 5,853
  • 21
  • 82
  • 148

1 Answers1

4

Since you are passing multiple command arguments in your 3rd argument, shell will do splitting as you are using unquoted variable inside your function.

You should better use shell array to store PARAMETERS argument.

Have your function as:

testing() {
   local url="$1"
   local httpType="$2"

   shift 2 # shift 2 times to discard first 2 arguments
   local params="$@" # store rest of them in a var

   response=$(curl -s -w "HTTPSTATUS:%{http_code}" -X "$httpType" "$url" "$params")
   echo "$response"
}

and call it as:

url='https://google.com'
httpType='POST'

params=(-F 'name=test' -F 'query=testing')
result=$(testing "$url" "$httpType" "${params[@]}")

Additionally you should avoid using all-caps variable names in your shell script to avoid clashes with env variables.


In case you're not using bash, you can also use:

params="-F 'name=test' -F 'query=testing'"
result=$(testing "$url" "$httpType" "$params")

Code Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I am getting "Syntax error: "(" unexpected" in this line - PARAMETERS=(-F 'name=test' -F 'query=testing') – Galet Aug 05 '17 at 10:18
  • I am using shell script only for all my script files. I can't change to bash. It will affect my whole scripting. – Galet Aug 05 '17 at 10:20
  • Check edited answer though changing `sh` to `bash` usually doesn't break anything. – anubhava Aug 05 '17 at 10:22
  • I am getting sample.sh: 421: local: -F: bad variable name sample.sh: 421: sample.sh: 'name: bad variable name in this line local params=$3 – Galet Aug 05 '17 at 10:31
  • But there is no `local params=$3` in my answer. You will need to copy/paste my suggested function code as well from answer. – anubhava Aug 05 '17 at 10:32
  • I get error as "Reason:
     Missing required argument: value
    . " after updation of code. I believe My API application requests accepting like this only -F "name=limit" -F "value=1000" instead of -F 'name=limit' -F 'value=1000'. I am not sure what is the exact issue here.
    – Galet Aug 07 '17 at 05:45
  • You can also reverse the quotes to make it: `params='-F "name=test" -F "query=testing"'` – anubhava Aug 07 '17 at 06:17
  • Still it doesn't solve. See here < HTTP/1.1 100 Continue } [270 bytes data] < HTTP/1.1 400 Missing required argument: value < Date: Mon, 07 Aug 2017 06:21:14 GMT < Cache-Control: must-revalidate,no-cache,no-store < Content-Type: text/html; charset=ISO-8859-1 < Content-Length: 349 * HTTP error before end of send, stop sending < { [349 bytes data] * Closing connection 0 – Galet Aug 07 '17 at 06:22
  • I am passing like this '-F "name=limit" -F "value=800"' – Galet Aug 07 '17 at 06:23
  • [It works well here in demo](http://rextester.com/YKDD93746) and on my shell as well. – anubhava Aug 07 '17 at 06:24
  • You should edit the question and copy/paste your latest script. Also try executing your script in bash for *testing purpose* – anubhava Aug 07 '17 at 06:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151234/discussion-between-karan-and-anubhava). – Galet Aug 07 '17 at 06:27
  • My curl request works manually when I hit PUT request with parameters. – Galet Aug 07 '17 at 06:43
  • Can you join this room https://chat.stackoverflow.com/rooms/151235/anubhave-and-karan – Galet Aug 07 '17 at 06:45