Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Chrome's "Copy as cURL" for multipart/form-data post requests on Windows?

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?

like image 510
Kleidersack Avatar asked Jun 25 '15 10:06

Kleidersack


People also ask

How do you send a POST request using cURL command?

When the -F option is used, curl sends the data using the multipart/form-data Content-Type. Another way to make a POST request is to use the -d option. This causes curl to send the data using the application/x-www-form-urlencoded Content-Type.

How do I copy cURL request in Chrome?

From Chrome On the line of the specific resource you're interested in, you right-click with the mouse and you select “Copy as cURL” and it'll generate a command line for you in your clipboard. Paste that in a shell to get a curl command line that makes the transfer.

How do I copy and paste in cURL?

To use this feature: Load a page with the Network Panel open. Right click on resource and select Copy > Copy as cURL. Paste your clipboard contents into your terminal.


1 Answers

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/ .

like image 158
Daniel Stenberg Avatar answered Sep 18 '22 23:09

Daniel Stenberg