Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the request library to match the requests_toolbelt library request to upload a file, the second request cannot be passed #5270

Closed
longweiqiang opened this issue Nov 20, 2019 · 4 comments

Comments

@longweiqiang
Copy link

longweiqiang commented Nov 20, 2019

Using the request library to match the requests_toolbelt library request to upload a file, the second request cannot be passed.

from requests_toolbelt import MultipartEncoder
import requests

m = MultipartEncoder(fields={'upload': open('test.txt', 'rb')},
                     boundary='----WebKitFormBoundarytZTJQrWcjjcJIMVQ')
params = {'path': 'test.txt',
          'token': '123456',
          'num': 0, 'offset': 0,
          'limit': 8}
response = requests.post('http://httpbin.org/post',
                         params=params,
                         data=m,
                         headers={'Content-Type': m.content_type})
# print("1: ", response.text)
# print("2: ", response.request.body)
# print("3: ", response.request.headers)

print(2)
response1 = requests.post('http://httpbin.org/post',
                         params=params,
                         data=m,
                         headers={'Content-Type': m.content_type})
  • Note:The request for the response1 variable cannot be completed, where test.txt is an empty file
@longweiqiang
Copy link
Author

Using the fiddler grab package, you find that after the second request, the body is missing after you enter the requests library

@longweiqiang
Copy link
Author

First request:
image

Second request:
image

@nateprewitt
Copy link
Member

Hi @longweiqiang, I believe the issue you’re seeing here is that you’re trying to resend a file after you’ve already read to the end. If the toolbelt isn’t automatically doing this, you’ll either need to move the pointer to the beginning of the file or reopen it.

@longweiqiang
Copy link
Author

Hi @longweiqiang, I believe the issue you’re seeing here is that you’re trying to resend a file after you’ve already read to the end. If the toolbelt isn’t automatically doing this, you’ll either need to move the pointer to the beginning of the file or reopen it.

thanks,It's working fine

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants