15
votes

I am developing a module for a web application. To trigger this module, I need to submit some data to the server. For simple forms, Copy as cURL in the Chrome Developer Tools works fine (using curl from msys[git]), but for post requests with multipart/form-data, the copied string is neither usable in the windows shell (cmd) nor with bash (form msys); the copied text is similar to:

curl "http://myserver.local" -H "Origin: http://wiki.selfhtml.org" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryntXdlWbYXAVwCIMU" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Cache-Control: max-age=0" -H "Referer: http://wiki.selfhtml.org/extensions/Selfhtml/frickl.php/Beispiel:HTML_form-Element1.html" -H "Connection: keep-alive" --data-binary "------WebKitFormBoundaryntXdlWbYXAVwCIMU"^
"Content-Disposition: form-data; name=""area"""^

"multi"^
"line"^
"------WebKitFormBoundaryntXdlWbYXAVwCIMU--"^
"" --compressed

Is there any way I could use this or convert it to something usable?

1
Shouldn't there be another ^ at the blank line?Rob W
Unfortunately neither with nor without the ^ this command is interpreted as one command (tried withcmd and bash).Kleidersack
I suggest to create a bug report at crbug.com/new and tell the team that the Copy as cURL command is unusable on Windows. Let me know when you've created the ticket.Rob W
Nice bug report. It'd be nice if you also attach a screenshot that shows what actually happens.Rob W

1 Answers

29
votes

Chrome, as well as the other browsers actually, do a rather poor job of translating multi-part formposts into curl command lines.

A much more convenient curl command line would not use --data-binary for that, it would use --form. And then you want one --form per input field.

In your case, it probably would look something like (backslashes inserted here for visibility):

curl "http://myserver.local" \
 --compressed \
 -H "Origin: http://wiki.selfhtml.org" \
 -A "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36" \
 -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" \
 -H "Cache-Control: max-age=0" \
 -e "http://wiki.selfhtml.org/extensions/Selfhtml/frickl.php/Beispiel:HTML_form-Element1.html" \
 -F area=[contents]

I left the [contents] in there, but it should be replaced with what you actually want in the area field. You could also pass it from a file if you prefer to.

I removed two unnecessary -H uses, and I replaced two to use the direct curl options.

h2c - headers to curl

Advice for the future: figure out the exact HTTP header trace you want to reproduce with a curl command line and paste it over at https://curl.se/h2c/ .