1

I have made a post requests with multipart/form-data; boundary=a1c2469c-2a1f-48e6-8f1d-311f8650c855 And I get this

POST /api/feed/upload-resource HTTP/1.1
App-Id: 15762288
Version-Name: 3.26.0.451
User-Agent: Right-Android/3.26.0.451
Content-Type: multipart/form-data; boundary=a1c2469c-2a1f-48e6-8f1d-311f8650c855
Content-Length: 75412
Connection: Keep-Alive
Accept-Encoding: gzip

--a1c2469c-2a1f-48e6-8f1d-311f8650c855
Content-Disposition: form-data; name="h"
Content-Length: 4

1080
--a1c2469c-2a1f-48e6-8f1d-311f8650c855
Content-Disposition: form-data; name="w"
Content-Length: 4

1080
--a1c2469c-2a1f-48e6-8f1d-311f8650c855
Content-Disposition: form-data; name="image"; filename="/storage/emulated/0/Android/data/com.xiaoyu.rightone/tiny/tiny-738-2018-08-03-15-32-53.jpg"
Content-Type: text/plain
Content-Length: 74910

If I want to simulate this request using Python requests package How can I do that.

I have checked the document, and I have tried use file parameter like this

with open(path, 'rb') as f:
    files = {
        "h": "1495",
        "w": "840",
        "filename": ("image", f.read()),
    }
    r = requests.post(url, files=files)

However in my situation, I always get an error like upload_resource_empty.

1 Answer 1

2

This should work:

import requests

files = {
    'image': ('file_name.jpg', open('file.jpg', 'rb'), 'text/plain'),
    'w': (None, '123'),
    'h': (None, '222')
}

response = requests.post('url', files=files)

The tuples in the dictionary have the following format: ('filename', fileobj, 'content_type', custom_headers)

Sign up to request clarification or add additional context in comments.

1 Comment

I have figured out that server side handle the w and h in post body. So I send w and h in post data, and solved. Thank you for your information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.