kaspian
kaspian

Reputation: 93

Run curl from .sh script with defined Content-Type

When I'm trying to run test.sh script I've always receive error from curl:

curl: (6) Couldn't resolve host 'application'

test.sh:

#!/bin/sh
CT="Content-Type:\ application/json"

TEST="curl http://127.0.0.1 -H $CT"
echo $TEST

RESPONSE=`$TEST`
echo $RESPONSE

But if I just run following command from console everything fine:

curl http://127.0.0.1 -H Content-Type:\ application/json

Could you please let me know what is wrong in script, as I understand something is wrong with 'space' escape, but have no idea how to fix it.

Also I've tried following combination, but result the same:

CT="Content-Type: application/json"
TEST="curl http://127.0.0.1 -H \"$CT\""

UPD:

bash / dash is only available on server. (/bin/sh --> bash)

GNU bash, version 4.2.10(1)-release (x86_64-pc-linux-gnu)

Upvotes: 9

Views: 59274

Answers (2)

Karirovax Karirax
Karirovax Karirax

Reputation: 11

You can try with: bash -c your_bash_file.sh It worked for me with the same problem

Upvotes: 1

Kuf
Kuf

Reputation: 17828

Run the following: (delete the space after Content-Type)

#!/bin/bash
CT="Content-Type:application/json"

TEST="curl http://127.0.0.1 -H $CT"
echo $TEST

RESPONSE=`$TEST`
echo $RESPONSE

Upvotes: 20

Related Questions