question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request headers getting added to files during upload PUT request from browser

See original GitHub issue

Hi @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 -

  1. Why the headers are being added?
  2. 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: screen shot 2017-06-29 at 9 51 25 pm

Superagent request object screen shot 2017-06-29 at 10 29 44 pm

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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
oyeanujcommented, Jun 30, 2017

@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 -

request.put(s3path)
  .set('Content-Type', 'image/jpeg')
  .set('Content-Disposition', 'inline')
  .send(theFile)     // <--------------------------- THIS LINE instead of .attach()
  .on('progress', updateCallback)
  .end(success, failure)

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!

1reaction
bcepurancommented, Jan 19, 2018

thank you @oyeanuj ! using .send is what I was missing.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found