48

I am trying to modify a curl request that was captured with Google Chrome Dev Tools.

Here is what the command looks like

curl "http://WEBSITE" -H "Host: WEBSITE" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" --compressed -H "Content-Type: multipart/form-data; boundary=---------------------------1184875127259" --data-binary "-----------------------------1184875127259"^

"Content-Disposition: form-data; name=""FORM1"""^

"FORM1DATA"^
"-----------------------------1184875127259"^

"Content-Disposition: form-data; name=""FORM2"""^

"FORM2DATA"^
"-----------------------------1184875127259"^

"Content-Disposition: form-data; name=""FORM3"""^

"FORM3DATA"^
"-----------------------------1184875127259"^

"Content-Disposition: form-data; name=""embed"""^

"true"^
"---------------------------1184875127259--"^
""

Form# is the name of the form and Form#Data is the data I submitted in the forms.

How would I make this be a single line curl request I can just copy into my command line and have it do the same thing that my browser did?

4 Answers 4

72

For Linux and MacOS: Use the \ escape character:

curl "http://WEBSITE" -H "Host: WEBSITE" \
-H "Accept: text/html,application/xhtml+xml \
,application/xml;q=0.9,*/*;q=0.8"

For Windows: Use the ^ escape character:

curl "http://WEBSITE" -H "Host: WEBSITE" ^
-H "Accept: text/html,application/xhtml+xml ^
,application/xml;q=0.9,*/*;q=0.8"
4
  • 10
    @DirkSchumacher, Instead of using \ for line escaping use ^ in windows. Back slash is for macOs.
    – Saini Arun
    Feb 5, 2019 at 9:58
  • @saini-arun : good hint!-) Thank you! Could have solved it by myself with little more cells in my brain... I just reflect for me that it is always good to share basic settings if I am aware that environmental issues could be considered. Feb 5, 2019 at 10:36
  • 1
    For Windows, you can use caret (^) as suggested above. Example: curl -H "X-Auth-Token: 9HqLlyZOugoStsXCUfD_0YdwnNnunAJF8V47U3QHXSq" ^ -H "X-User-Id: aobEdbYhXfu5hkeqG" ^ -H "Content-type:application/json" ^ -d '{"avatarUrl": "domain.tld/to/my/own/avatar.jpg"}' ^ localhost:3000/api/v1/users.setAvatar
    – Raghav
    Apr 27, 2020 at 7:06
  • Note that if attempting to import a cURL request in as "Raw Text" in Postman, use Copy >> "Copy as cURL (bash)" in Chrome Dev Tools to get the request in the format that Postman expects. "Copy as cURL (cmd)" uses the format that includes the ^ as line separator and Postman will throw an error reading: "Error while importing Curl: Only the URL can be provided without an option preceding it. All other inputs must be specified via options." Jul 16, 2020 at 15:41
6

If you are running Windows, I have found it easier to install Git and use Git Bash to run Curl. This was initially suggested in a separate article: https://stackoverflow.com/a/57567112/5636865.

1
  • 2
    I agree! Git Bash is the awesome when you are on Windows. Jan 20, 2021 at 16:03
4

NOTE: watch out for the tendency to indent on multiple line commands, as it will embed spaces and screw up the curl command. the sed command replaces embedded spaces within the variables with the %20 string so that spaces can be used embedded in the strings you pass as variables

messageout="The rain in Spain stays mainly in the plains"
summaryout="This is a test record"
alertnameout="Test Alert"


curl -v -silent request POST "URL.com?\
summary=`echo $summaryout | sed -e 's/ /%20/g'`&\
alertname=`echo $alertnameout | sed -e 's/ /%20/g'`&\
message=`echo $messageout | sed -e 's/ /%20/g'`"
4

The Windows equivalent to \ is ^.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.