1

I want to use cURL to test a RESTful web service resource.

In that effort, one of the headers requires a linebreak to properly pass the data to the server.

In addition to endlessly searching for what I would think would be fairly common to no avail, I have tried using (\n):

curl -X POST --header "login_id: testUser" --header "passcode: testPasscode" --header "dataHeader: some data\nsome more data"  http://localhost:8080/api/test

which does not work. The line break is not recognized when the header is read at the server - I think cURL actually removes it.

I have also tried using (%0A):

curl -X POST --header "login_id: testUser" --header "passcode: testPasscode" --header "dataHeader: some data%0Asome more data"  http://localhost:8080/api/test

How should I craft the post to insert a line break between some data and some more data?

I am using terminal on a Mac

Roy Hinkley
  • 10,111
  • 21
  • 80
  • 120
  • You might want to try to use both line feed and carriage return. So `--header "dataHeader: some data\r\nsome more data"` Also, you're missing closing quotes on that header string but I figured that's just a SO typo – paulski Jul 30 '13 at 19:09
  • Thanks but that did not work either. It appears both the line line and carriage return are removed when cURL encodes the header. – Roy Hinkley Jul 30 '13 at 19:15
  • Have you tried using a separate `--header` switch for the second line? – Paulo Almeida Jul 30 '13 at 19:25
  • The specification requires the data to be in one header. I don't make the rules, I am just trying to follow and subsequently test them. I can do this from Java, but would like to use cURL if possible. – Roy Hinkley Jul 30 '13 at 19:37
  • I don't think curl removes the `\n`. Try `curl -v -H "dataHeader: some data\nsome more data" -o /dev/null example.com`. – Paulo Almeida Jul 30 '13 at 19:46
  • Regarding @pauldom's comment, see [this question](http://stackoverflow.com/questions/5757290/http-header-line-break-style). But you already tried it anyway. – Paulo Almeida Jul 30 '13 at 19:54
  • One last shot - Have you tried just sending it with `\r`? [This question](http://stackoverflow.com/questions/771077/php-and-http-header-line-breaks-what-character-used-to-represent) makes me think that `\n` might be the problem. – paulski Jul 30 '13 at 21:59
  • Agreed. I have researched http protocol and the url-encoded new line character must be used. So I have refactored and will have to use `%0A`. Thanks for your help. – Roy Hinkley Jul 30 '13 at 22:13

1 Answers1

1

Try passing a raw line break from the command line:

$ curl -X POST [...] --header "dataHeader: some data<press ENTER>
> some more data" http://localhost:8080/api/test
Rômulo Ceccon
  • 10,081
  • 5
  • 39
  • 47
  • 1
    Type it the way it's written. Leave the double-quotes open, press enter, and continue on the second line. – Rômulo Ceccon Jul 30 '13 at 19:16
  • My confusion lies in that hitting enter "sends" the command and results in the terminal displaying "Unmatched "."" – Roy Hinkley Jul 30 '13 at 19:18
  • 1
    Which shell are you using? It works in bash and sh, at least. Leaving the quotes open should yield a second line with the PS2 prompt (usually ">"). – Rômulo Ceccon Jul 30 '13 at 19:21
  • Terminal on my Macbook Pro – Roy Hinkley Jul 30 '13 at 19:22
  • What happens when you type 'echo "test' (without the single quotes) on the command line? OSX should use Bash. – Rômulo Ceccon Jul 30 '13 at 19:25
  • [Macintosh-2:~] % echo "test" test [Macintosh-2:~] % – Roy Hinkley Jul 30 '13 at 19:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34484/discussion-between-romulo-ceccon-and-android-addict) – Rômulo Ceccon Jul 30 '13 at 19:27
  • The curl manual page says: "curl will make sure that each header you add/replace is sent with the proper end-of-line marker, you should thus not add that as a part of the header content: do not add newlines or carriage returns, they will only mess things up for you." – Paulo Almeida Jul 30 '13 at 19:28
  • I require a new line marker - not an end-of-line marker. The data that will be sent in production will have new line characters in it. I can do this by POSTing from Java, but I want to do this with cURL. – Roy Hinkley Jul 30 '13 at 19:36