Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

upload binary file using python requests

I am uploading a file using requests library below is the code:

files = {'file': open(full_file_name, 'rb')}
headers = {"content-type": 'application/x-www-form-urlencoded'}
final_resp = requests.put(loc, files=files, headers=headers)

The problem is some extra contents have been added to the file's start and end point.

The contents added to the start point is:

--b16010ae7646a031a5adc64ac0661e72
Content-Disposition: form-data; name="file"; filename="1016064585-65769268.csv"

The contents added to the endpoint is:

--b16010ae7646a031a5adc64ac0661e72--

But when the same file is uploaded through the postman these problems are not arising.

here is the screenshot of the postman enter image description here.

The header of the postman is:

application/x-www-form-urlencoded

like image 690
bSr Avatar asked Feb 06 '19 15:02

bSr


People also ask

How do I load a binary file in Python?

To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode opens the file in binary format for reading, while the "wb" mode opens the file in binary format for writing.

How do you upload a binary file?

To upload a binary file, create a multipart request with a part that contains the JSON request body required by the resource, and a part for each file you want to upload. Each file must have its own part. To upload binary data to multiple fields, add a part for each field. The part names must match the field names.

Can Python handle the binary files?

Python has tools for working with binary files. Binary files use strings of type bytes. This means when reading binary data from a file, an object of type bytes is returned. The binary file is opened using the open() function, whose mode parameter contains the character 'b'.

How do you upload a file in Python?

Method 1: Using the Python's os Module: Also, the enctype attribute with "multi-part/form-data" value will help the HTML form to upload a file. Lastly, we need the input tag with the filename attribute to upload the file we want. Lastly, we need the input tag with the filename attribute to upload the file we want.

How to upload files using Python requests library?

Here we open the file in read binary mode and pass the handler to post request. Python requests library accepts a parameter “ files ” to upload the files. import requests url = 'http://localhost/uploadfile' file = {"myfile":open('example.xml','rb')} #Upload File response = requests.post(url, files=file)

How do you read a binary file in Python?

Python read a binary file. Here, we will see how to read a binary file in Python. Before reading a file we have to write the file. In this example, I have opened a file using file = open(“document.bin”,”wb”) and used the “wb” mode to write the binary file. The document.bin is the name of the file.

How do I upload a file using venv in Python?

$ python3 -m venv . Activate the virtual environment so that we would no longer impact the global Python installation: Create a new file called single_uploader.py which will store our code. In that file, let's begin by importing the requests library: Now we're set up to upload a file!

What are the best Python libraries for data transfer over HTTP?

Python is supported by many libraries which simplify data transfer over HTTP. The requests library is one of the most popular Python packages as it's heavily used in web scraping. It's also popular for interacting with servers! The library makes it easy to upload data in a popular format like JSON, but also makes it easy to upload files as well.


1 Answers

it may because you use multipart/form to upload file.try use data like code below

data = open(localFilePath, 'rb').read()
headers = {
    "Content-Type":"application/binary",
}
upload = requests.put(uploadUrl,data=data,headers=headers)
like image 131
shutup Avatar answered Oct 19 '22 02:10

shutup