Skip to content

Instantly share code, notes, and snippets.

@apast
Created August 18, 2018 15:01
Show Gist options
  • Save apast/842ce33ea3a144c2e033a13ed6d4c8d4 to your computer and use it in GitHub Desktop.
Save apast/842ce33ea3a144c2e033a13ed6d4c8d4 to your computer and use it in GitHub Desktop.
Reading about how curl handle and encode parameters to understand what happened on requests with values containing spaces, like following

curl -vvv -X POST "http://localhost:4000/set?somekey=spaced value"

Running this command, we have:

POST /set?key=k ey HTTP/1.1 Host: localhost:4000 User-Agent: curl/7.61.0 Accept: /

< HTTP/1.1 400 Bad Request

In this example, curl doesn't encode URL data automatically and we have the following error on server log:

INFO:tornado.general:Malformed HTTP message from ::1: Malformed HTTP request line

To send this as valid requests, as expected from the begining, it should be send with two main changes:

  1. Split the querystring part and move to option --data-urlencode. Using this option, curl automatically consider this request as POST;
  2. Add -G option to send all --data-urlencode value on querystring, as GET. It can still be combined with -XPOST option to send as POST.

The final command is:

curl -vvv -G -X POST "http://localhost:4000/set" --data-urlencode "somekey=spaced value"

Running this, we have the encoded data being sent on the query string part:

POST /set?key=k%20%20y HTTP/1.1 Host: localhost:4000 User-Agent: curl/7.61.0 Accept: /

< HTTP/1.1 201 Created

On server LOG, now, we receive:

INFO:tornado.access:201 POST /set?key=k%20%20y (::1) 0.91ms

The reference where I found this helpful info, was the following link: https://www.systutorials.com/241492/how-to-encode-spaces-in-curl-get-request-url-on-linux/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment