How can I upload a file using `curl` instead of the form?

Chuck

I have a client who's customer uses a third party clearinghouse to receive invoices. We can do this manually by exporting our data to XML, going to a particular website, and uploading the file. The relevant HTML form looks like this:

<form border=0
      method="POST"
      action="/server?request=send"
      enctype="multipart/form-data"
      id="uploadform"
      name="uploadform"
      onsubmit="return checkUpload()">
  <H2>Upload</H2>
  <p>Server directory:<br>
  <input type="text"
         id="directory"
         name="directory"
         value="./"
         size="30"></p>
  <p>Local file to be uploaded:<br>
  <input type="file"
         id="file"
         name="file"
         size="40"></p>
  <p><input type="submit" value="Send"></p>
</form>

Manual submission using their web interface works fine. I'm trying to write a quick tool that submits the form for us using curl. I've tried many different attempts, but so far, each one, although I get a "File successfully uploaded" HTML response, the file they get is empty.

We originally started trying to use curl because it was how the clearinghouse company suggested we do so. The example they gave was this:

curl -i -k -H "Content-Type:application/octet-stream" \
  -d @test.txt -u username:password \
  https://example.com/server?request=send?filename=test.txt?directory=DX001

When that didn't work, I began looking at the code in the form above, and tried the following (while in the same directory as the XML file):

curl -F "request=send" -F "directory=DX001" -F "file=33823.xml" \
  -u username:password -F "enctype=\"multipart/form-data\"" \
  -F "id=\"uploadform\"" https://example.com/server

When that didn't work, I thought I'd try the absolute path even though I was in the same directory:

curl -F "request=send" -F "directory=DX001" -F "file=/Users/chuck/Desktop/33823.xml" \
  -u username:password -F "enctype=\"multipart/form-data\"" \
  -F "id=\"uploadform\"" https://example.com/server

Every single one of these only uploads an empty file (with the given file's name).

As you can probably tell from the full path, I'm doing this from macOS, in case that's relevant.

Note, the onsubmit action within the form simply ensures that neither the directory nor the file inputs are empty.

user1686

Every single one of these only uploads an empty file (with the given file's name).

Curl doesn't know which fields are regular text and which are file upload; it assumes the former unless you specifically tell it to do the latter. To attach a whole file, you need the @ prefix:

-F "file=@/Users/chuck/Desktop/33823.xml"

You can give the server a different filename like this:

-F "file=@\"/Users/chuck/Desktop/temp.xml\";filename=\"33823.xml\""

The correct full command should be:

curl -u username:password \
     -F "directory=DX001" \
     -F "file=@/Users/chuck/Desktop/33823.xml" \
     https://example.com/server?request=send

Note how request=send is not part of the form data; it was part of the URL before, it is still part of the URL now. Finally, although your examples all include -u for authentication, remember that it only works for the HTTP 'Basic' mechanism, not for form/cookie-based login pages.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to can I upload a form on django

How can I upload a file to server, without browser, using go?

how to upload file using curl with php

How can I upload a file to rackspace using RESTSharp and .net 4.0?

How can I upload a file and form data using Flurl?

using curl for multipart/form-data with a file upload

How can I read a file which will be upload from a form in .Net Core API?

How to upload file from local to server using curl in php?

how can i upload file by using xpath in cypress

How can I upload files to GoogleDrive in "multipart" type by using php-curl?

How can I upload a file to Dynamics?

How can I upload a zip file using nodejs and extract it?

How can I upload the image file with axios?

How do I receive a file upload in spring mvc using both multipart/form and chunked encoding?

Can't upload file to a server using curl

How can I unmap a File input without using the form builder?

How can I upload a file or we can say choose a file from the system in Visual C++ Form Application

How can i upload a file into a ftp server?

How do I serialize the form's file to upload so that it can be stored in a mongo db, using Backbone.js, jQuery and Node.js?

how can i use curl instead of fopen

How do I validate if a file is selected for upload before submitting a form , using jquery validator

Can I download or upload file using CURL for file://///my_windows/test (non-http link)

How can I check if file has been chosen to upload before submitting form

How can i upload a docx file on server using express js?

How I can create a formless file upload using Javascript?

How do I upload a file in Django OneToOne Model's FileField using form?

How can I mock a file upload in nestjs?

How can I upload a file in an FTP server using javascript?

How can write curl command to upload file into dropbox with refresh code?