Request headers getting added to files during upload PUT request from browser
See original GitHub issueHi @pornel! Thank you for this excellent library. So, I was trying to upload files to S3, using PUT (to a pre-signed url) and ran into the following issue. I looked through StackOverflow and previous issues, and couldn’t find anything which resolved this, hence this ticket.
The file I was uploading is getting corrupted. On investigation, it seems that is because the following headers are being appended to the file.
I was unable to figure out -
- Why the headers are being added?
- In the headers being added to the file, the
Content-Disposition
is different than that being set in the Request Header. Why is that?
Here are a couple of screenshots: Headers added in the payload:
Superagent request object
Following the docs, here is how I am making the request from my browser:
request.put(s3path)
.set('Content-Type', 'image/jpeg')
.set('Content-Disposition', 'inline')
.attach(fileSlug, filename.jpg)
.on('progress', updateCallback)
.end(success, failure)
And if you think a doc clarification is needed, I’ll be happy to PR!
Thank you!
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Request headers getting added to files during upload PUT ...
The file I was uploading is getting corrupted. On investigation, it seems that is because the following headers are being appended to the...
Read more >Uploading Files from a Web Browser - ScaleOut Software
Only one file may be uploaded per request. Unlike most operations in the REST API, POST requests do not rely on HTTP headers...
Read more >How does HTTP file upload work? - Stack Overflow
Open the HTML on your browser, select the files and click on submit and check the terminal. nc prints the request received.
Read more >Content-Disposition - HTTP - MDN Web Docs
In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline ...
Read more >HTTP Request Methods – Get vs Put vs Post Explained with ...
We use POST to create a new resource. A POST request requires a body in which you define the data of the entity...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@pornel @focusaurus Thank you for taking a look! I found the root issue, and the solution (in case anyone else runs into this).
The content boundary header added seems to be the default browser behavior whenever attaching files, or submitting a multi-part request (so, removing the content-headers above didn’t matter).
It seems that typically webservers know how to strip that out when dealing with that upload, but not S3. S3 (and other object stores) requires the files to be uploaded as the main body/data. So, instead of the above method, the following works -
At this point, the browser sends this as the main body of the request, rather than assuming it to be
multipart/form-data
request (which seems to be the default behavior).Let me know if you guys think this sort of case needs to be mentioned in the main superagent homepage somewhere as an example!
thank you @oyeanuj ! using .send is what I was missing.