0

I attempt to use Curl to send some data. Here is a most simple example that works:

curl -v -H "Content-type: text/html" 127.0.0.1:1111

I see output like this:

GET / HTTP/1.1
Host: 127.0.0.1:1111
User-Agent: curl/7.49.1
Accept: */*
Content-type: text/html

Content-type is set there in the output. It works. What I want is to input content type using environment variable.

I write this:

HTML='-H "Content-Type: text/html"

Then in the next command I write this:

curl -v $HTML 127.0.0.1:1111

But it failed. I am given this output:

GET / HTTP/1.1
Host: 127.0.0.1:1111
User-Agent: curl/7.49.1
Accept: */*
  "Content-Type: text/html"

Curl is not making a real header, it is just loose string. It has quotes around it, and is indent like you can see above. This is different from before and so the command does not work. It is rejected by server as a wrong content type.

I have tried using --header in stead of -H like this:

HTML='--header "Content-Type: text/html"'

This gives a confusing error:

curl: option --header "Content-Type: text/html": is unknown
curl: try 'curl --help' or 'curl --manual' for more information

I am confused. I have seen other messages here from people who want environment variable inside post data, which is a different problem.

I am using zsh and Curl on MAC OS X. I hope my example is simple enough.

Thankyou for your time and help!

1 Answer 1

2

This behaviour is due to the fact that zsh does not split parameters on whitespaces by default, so you really pass --header "Content-Type: text/html" to curl as one parameter, whereas it should be passed as two (first the `--header`` option and then the corresponding value).

You can advice zsh to do a word splitting (which also takes the nested quoting in account which is important in your example) by using the (z) expansion flag:

HTML='--header "Content-Type: text/html"'
curl -v ${(z)HTML} 127.0.0.1:1111

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .