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.

Set http headers on per file basis?

See original GitHub issue

Hi, Can I set custom http headers on per file basis? E.g. I would like to send original file name in http header. I use XHR Upload to upload files and I set formData to false, because I don’t want to deal with a multipart form on the server. Now have to figure it out how to send file names and possibly some other data (modification date might also be useful).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
StasDcommented, Jun 8, 2020

This is exactly what I was looking for! Thanks!

I put the code inside 'file-added' event handler, as file already exists there (no need to call uppy.getFile). Also decided not to include ...file.xhrUpload as this property doesn’t exist at that point.

    this.uppy.on('file-added', (file) => {
      this.uppy.setFileState(file.id, {
        xhrUpload: {
          headers: {
            'Original-File-Ext': file.extension,
            'Original-File-Name': encodeURIComponent(file.name),
            'Original-File-Date': file.data.lastModified,
          },
        },
      });
    });

Also had to encode file names using encodeURIComponent() function, as it turned out that uppy doesn’t even start uploading files if I set a header e.g. 'Original-File-Name': file.name and file name contains unicode characters.

On server side I then do this (in asp.net controller):

var headers = Request.Headers;
var ext = headers["Original-File-Ext"].ToString();
var origName = Uri.UnescapeDataString(headers["Original-File-Name"].ToString());
string origDateModified = headers["Original-File-Date"];

All looks quite nice to me and works great! I don’t think you need to do anything extra on this matter. 😃

1reaction
goto-bus-stopcommented, Jun 8, 2020

There is not a great way to do this, but there is a way. You can use setFileState to assign headers:

const file = uppy.getFile(fileID)
uppy.setFileState(fileID, {
  xhrUpload: {
    ...file.xhrUpload,
    headers: { 'App-File-Name': file.meta.name }
  }
})

Not the prettiest but it should work. You could do this inside an uppy.on('upload') event handler to make sure that the value is updated right before it’s being uploaded. file.xhrUpload is an undocumented feature but we rely on it in several plugins so it won’t go away in a minor release.

This is a common feature request that we’ll hopefully be able to address in a nicer way at some point 😅

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP headers - MDN Web Docs - Mozilla
HTTP Client hints are a set of request headers that provide useful information about the client such as device type and network conditions, ......
Read more >
How to Add Custom HTTP Headers to a File or Location
By adding HTTP headers to a file or location, you can provide additional ... This is because Nginx config works on a per...
Read more >
Setting headers with an .htaccess file
You can use an .htaccess file to adjust or add headers to your HTTP response headers. Creating an .htaccess file on your DreamHost...
Read more >
Adding or modifying headers on HTTP requests and responses
Create new headers · In the Name field, enter the name of your header rule (for example, My header ). · From the...
Read more >
Configuring HTTP headers to maintain session state - IBM
To configure HTTP headers to maintain session state, specify the header names in the [session-http-headers] stanza of the WebSEAL configuration file.
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