-
-
Notifications
You must be signed in to change notification settings - Fork 954
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
Issue with MultiPartParser class #1059
Comments
I have found the issue. In the As per HTTP convention, In the meantime this is what I have managed to write to allow the both files to be parsed from the request: @app.post("/upload")
async def upload(request: Request):
body = await request.body()
request._body = body.replace(b'\r\n', b'\n').replace(b'\n', b'\r\n')
inp = await request.form()
fasta = inp["fasta"]
attributes = inp["attributes"] |
Okay, so a useful point to start with would be double checking how other frameworks handle the malformed input here. What client are you using? |
Hi @tomchristie ! Thank you for replying. So before Starlette the service was using Flask and there were no issues - so Flask can handle the 'malformed' input. I have also tried issuing a POST with the 'malformed' body to import http.client
import mimetypes
conn = http.client.HTTPSConnection("httpbin.org")
client_boundary = 'xoxoxoxoxoxoxoxoxoxo_1600710720987'
postman_boundary = '--------------------------850116600781883365617864'
client_payload = '--xoxoxoxoxoxoxoxoxoxo_1600710720987\r\nContent-Disposition: form-data; name="fasta"; filename="sequences.fasta"\r\nContent-Type: multipart/form-data\r\n\r\n>P23A07_IgM-1047:H:Q10C92:17/22:NID2093\nCAGGTTTCAGTAG\n--xoxoxoxoxoxoxoxoxoxo_1600710720987\r\nContent-Disposition: form-data; name="attributes"; filename="attributes.tsv"\r\nContent-Type: multipart/form-data\r\n\r\n"Campaign ID"\t"Plate Set ID"\t"No"\n\r\n--xoxoxoxoxoxoxoxoxoxo_1600710720987--\r\n'
postman_payload = '----------------------------850116600781883365617864\r\nContent-Disposition: form-data; name="attributes"; filename="test-attribute_5.tsv"\r\nContent-Type: text/tab-separated-values\r\n\r\n"Campaign ID"\t"Plate Set ID"\t"No"\n\r\n----------------------------850116600781883365617864\r\nContent-Disposition: form-data; name="fasta"; filename="test-sequence_correct_5.fasta"\r\nContent-Type: application/octet-stream\r\n\r\n>P23G01_IgG1-1411:H:Q10C3:1/1:NID18\r\nCAGGTATTGAA\r\n\r\n----------------------------850116600781883365617864--\r\n'
client_headers = {
'Content-type': 'multipart/form-data; boundary={}'.format(client_boundary)
}
postman_headers = {
'Content-type': 'multipart/form-data; boundary={}'.format(postman_boundary)
}
conn.request("POST", "/post", client_payload, client_headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
conn.request("POST", "/post", postman_payload, postman_headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8")) It returned both files and does recognise non-standard line breaks in the body. The client is a KNIME pipeline and throughout execution sends HTTP calls using Palladian nodes. I have no access to the client code so I had to look for ways to overcome the issue using Starlette instead. |
I would be interested to make a PR on this but can't decide whether it should be here. Knowing that Starlette uses |
@tomchristie any update on this ? I'm using the requests library from python and I cannot manage to upload files, I always get a #1514 seemed to address this issue nicely given that the Does anyone currently have a workaround to use Thanks in advance for your reply |
I still find it strange that after all this time since the issue has been raised the maintainers of @hall-b |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
We've decided to vendor |
This decision was postponed. |
PR welcome on |
I've come across this issue when writing a test for endpoint that accepts a file. Because I am in control of the payload in this case, this is a client side workaround that worked for me - using requests to format the request body: import requests
with open(file_path, "rb") as f:
body, content_type = requests.PreparedRequest()._encode_files(files={"file": f}, data={})
response = test_client.post("/document/new", content=body, headers={"Content-Type": content_type}) |
Checklist
master
.Describe the bug
MultiPartParser
class fails to parse a request body produced by the client but not Postman (or similar).To reproduce
Expected behavior
client_test_multipart
should read and return two files from theclient_body
as withpostman_test_multipart
andpostman_body
.Actual behavior
client_test_multipart
reads only one file from the request body produced by the client.Debugging material
Environment
The text was updated successfully, but these errors were encountered: