Fractaliste
Fractaliste

Reputation: 5957

How to send multipart/mixed with CURL with predifined boudary?

I need to send files to a server which contains multipart/mixed content. For exemple:

------------------------------1b479dca9c3e
Content-Disposition: form-data; name="one_part"
Content-Type: text/xml; charset=utf-8
XX-Project-Type: METADATA

<?xml version="1.0" encoding="utf-8"?>
<My_MSG version="1">
  <MSG date="2014-08-20T18:39:59.154326+00:00">
    <METADATA mess_id="sd1212sd05AZ">
      <CONVERSION>
        <FILE name="foo.bar"/>
        <FILE name="ping.pong"/>
        <FILE name="abc.def"/>
      </CONVERSION>
    </METADATA>
  </MSG>
</My_MSG>

------------------------------1b479dca9c3e
Content-Disposition: form-data; name="one_part"
Content-Type: text/xml; charset=utf-8
XX-Project-Type: METADATA

<?xml version="1.0" encoding="utf-8"?>
<My_MSG version="1">
  <MSG date="2015-08-20T19:39:59.154326+00:00">
    <METADATA mess_id="az987456321">
      <CONVERSION>
        <FILE name="bar.foo"/>
        <FILE name="pong.ping"/>
        <FILE name="def.abc"/>
      </CONVERSION>
    </METADATA>
  </MSG>
</My_MSG>

------------------------------1b479dca9c3e--

As you can see the multipart boudary is already defined into the file to send, so to send it with CURL I use following command:

curl -d @/tmp/exemple.file -XPOST http://myServer:8000/multipartService -H "content-type: multipart/Mixed ; boundary=----------------------------1b479dca9c3e"

But the server does not succeed to handle parts. For the moment I don't have server's logs, but I perform a tcpdump to see what is send to the server:

tcpdumpResult

It seems that spaces and carriage returns are deleted in the send request and it could explain that the request content can't be handle as multipart/mixed by the server...

Do you know how to send my file with multipart/mixed compliant format?


Edit for hanshenrik:

enter image description here

Upvotes: 0

Views: 3205

Answers (2)

Tim Seed
Tim Seed

Reputation: 5279

Using Flask Frame work I receive the data in the /add API endpoint.

@app.route('/add', methods=['POST'])
def add():
    if request.headers['Content-Type'].startswith('multipart/form-data'):
        print("multi-form data")
        json_data=request.form.get("json_data")
        jd = json.loads(json_data)
        file_data=request.form.get("file")
        return "200 Ok" 

I send using CURL the data - a TEXT file in my case - Binary will be a different case. Like this

#/bin/bash
# Note 2 variables file and json
# file is loaded using < redirection
# Json_Data is a hand crafted dictionary
curl -X POST -H "Content-Type: multipart/form-data" \
     -F "file=<MixedContent.sh" \
     -F "json_data={\"username\":\"xyz\",\"password\":\"xyz\"}" \
     -X POST http://127.0.0.1:5000/add

Take care with the -F "json_data" It seems very fussy about extra ' and "s

Upvotes: 0

Daniel Stenberg
Daniel Stenberg

Reputation: 57994

Let curl do the multipart POST itself and let it handle the separator completely by itself instead. If you for example want to post two parts with contents read from two different files, do it like this:

curl -F "part_one=<file1.xml" -F "part_two=<file2.xml" \
http://myServer:8000/multipartService

Insisting?

Then you need to do the whole thing yourself and use --data-binary.

Upvotes: 1

Related Questions