How to send request body along with boundary delim

2019-07-21 02:59发布

问题:

I am new to curl. I am using curl to upload a file using shell script. I have to set the boundary delimiters explicitly and make an HTTP request by passing few fields.

I have couple of questions:

  1. Is it possible to explicitly set boundary delimiters using GET method? If yes, how?

  2. If not then is it okay to use something like this in a POST method (check the arguments to the --data)

curl --data "--4ebf00fbcf09&AB=MSG&tableView=simple&orient=UP&x-895892-n=1 &x-128754-o=2&x-974573-p=1&client=browser&d-984303-s=create&--4ebf00fbcf09--" --header "Content-Type: multipart/form-data; boundary=4ebf00fbcf09"
www.xyz.com

Many Thanks!

回答1:

Those delimiters are used for multipart formposts, and curl will handle the delimiters automatically for you if you just make sure to use --form instead of --data.

The difference between curl's -d and -F is also explained in the curl book in section aptly titled "-d vs -F".

The manual way

If you truly want to set the boundary yourself, you need to build the entire formpost yourself as RFC1867 dictates.

A shell script to send the two values "name=foo" and "value=42":

#/bin/sh
boundary="------------------------2100d47bb8839948"

# generate the body
echo "$boundary" > post
echo 'Content-Disposition: form-data; name="name"' >> post
echo "" >> post
echo "foo" >> post
echo "$boundary" >> post
echo 'Content-Disposition: form-data; name="value"' >> post
echo "" >> post
echo "42" >> post

## final boundary uses two extra dashes
echo "$boundary--" >> post

curl -H "Content-Type: multipart/form-data; boundary=$boundary" localhost --data-binary @post


标签: http curl