SoFunction
Updated on 2024-12-19

Python request post file upload common points

common usage

However, the field name in the above figure, the type needs to be filled according to different interfaces, such as a service interface:

The corresponding upload code is thus as follows:

# Output parameters: request response message
import requests
request_url = 'https://XXXXX/file-upload'
head = {
    # "Content-Type": "multipart/form-data; boundary=.3c7024a080e6a27f", # Note, do not write Content-Type here
"apiVersion": "v1.0.0",
    "appVersion": "v1.0.0",
    "OS": "iOS",
    "OSVersion": "11.4.1",
    "language": "zh",
    "did": "NTIzOTIxNDYwMzMyM2NjZjk1OGM5NjBmYzNlNzg2OTYtZTkxNzg1MzYzNjA4NGM0Mjg4Njg3MmFhNzExMDE1YTgwMDAyLXIwWUtuK0MrS1Y2eDBteWs3WnhDYmQ2ZnovTT0=",
    "reqSeq": "eb144eed4639d36bb6a7b9aa2a563421",
    "timestamp": "1545789471",
     "userToken":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJiaXpUeXBlIjoiTE9BTiIsInVzZXJSb2xlIjoyLCJleHAiOjE1NTQ2MTYwMjUsInVzZXJJZCI6MzAxMzkyNDUxNTc3Mzk3MjQ4LCJjaGVja0NvZGUiOiJiZTk1MDEwZjczN2Q0YTU2ODkxNTYyMDBlNDhhZDEyZCJ9.97Km63wUC6IaoSYE1Db8fAxYoW5N-ZQkbiw4kETN2cQ"
}

fl = open('','rb')
files = {'files': ('', fl, 'application/octet-stream', {'Expires': '0'})} #The field name files and type are consistent with application/octet-stream and the captured interface.
r2 = (request_url, headers=head, files=files)

print(('POST', request_url, headers=head, files=files).prepare().('ascii')) #You can print out the field name and type of the real request, and if it doesn't match the crawler interface, adjust the
print()

This is the whole content of this article, I hope it will help you to learn more.