javascriptnode.jsexpresshttphttp-headers

NodeJS with express: Header undefined


I'm using Microsoft Flow to send an HTTP POST request to my server. The request body contains a custom header called "Email-To", which has a string as it's value. Here is the body:

{
  "$content-type": "multipart/form-data",
  "$multipart": [
    {
      "headers": {
        "Email-To": "test@email.no",
        "Content-Disposition": "form-data; name=\"file\"; filename=901210.xlsx"
      },
      "body": {
        "$content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "$content": "content"
      }
    }
  ]
}

When the request reaches my NodeJS server, I want to retrieve the value from the header.

app.post('/upload', function(req, res) {
  console.log(req.headers("Email-To"))
  console.log(req.get("Email-To"))
});

Both the logs will always print "undefined". I tried debugging it further by looking at the request object itself, under headers. It doesn't seem to have the header.

0|server  |   headers:
0|server  |    { host: 'my_ip',
0|server  |      'accept-language': 'en-US',
0|server  |      'user-agent':
0|server  |       'azure-logic-apps/1.0 (workflow aa9d5e05e9b54c478ccebf2412633add; version 08586366094673048593) microsoft-flow/1.0',
0|server  |      'x-ms-execution-location': 'westeurope',
0|server  |      'x-ms-workflow-id': 'aa9d5e05e9b54c478ccebf2412633add',
0|server  |      'x-ms-workflow-version': '08586366094673048593',
0|server  |      'x-ms-workflow-name': '010bf24d-5a56-4053-8a39-e088e727cbf5',
0|server  |      'x-ms-workflow-system-id':
0|server  |       '/locations/westeurope/scaleunits/prod-48/workflows/aa9d5e05e9b54c478ccebf2412633add',
0|server  |      'x-ms-workflow-run-id': '08586365350241297663352938044CU52',
0|server  |      'x-ms-workflow-run-tracking-id': '2eec16eb-2f3f-4a87-a26d-683750ca20f5',
0|server  |      'x-ms-workflow-operation-name': 'HTTP',
0|server  |      'x-ms-workflow-subscription-id': 'b745f25e-91b5-4140-9d73-93b10b1dfb1d',
0|server  |      'x-ms-workflow-resourcegroup-name':
0|server  |       'B5EBEC49F06342E5B0E1F4E04C03D0B7-D8266BA594DC4A5DB55D7F31650FCD73',
0|server  |      'x-ms-tracking-id': 'aa92c109-151a-418c-8d01-754da7bc2c43',
0|server  |      'x-ms-correlation-id': 'aa92c109-151a-418c-8d01-754da7bc2c43',
0|server  |      'x-ms-client-request-id': 'aa92c109-151a-418c-8d01-754da7bc2c43',
0|server  |      'x-ms-client-tracking-id': '08586365350241297663352938044CU52',
0|server  |      'x-ms-action-tracking-id': 'f3a02477-227d-46cf-9105-228fe5c41f8d',
0|server  |      'x-ms-activity-vector': 'IN.0E',
0|server  |      'content-type':
0|server  |       'multipart/form-data; boundary="c859bba4-c9e1-4426-86ce-b3daf6ab74d9"',
0|server  |      'accept-encoding': 'gzip, deflate',
0|server  |      via: '1.1 my_ip (Apache/2.4.18)',
0|server  |      'x-forwarded-for': 'someones_ip',
0|server  |      'x-forwarded-host': 'my_ip',
0|server  |      'x-forwarded-server': 'my_ip',
0|server  |      connection: 'Keep-Alive',
0|server  |      'content-length': '43929' }

After reading up on headers, I'm pretty sure any key-value pair that doesn't conflict with the other headers should work fine, yet my "Email-To" header doesn't seem to work. Any ideas why?

Edit: CORS (using wildcard)

Sending POST request to http://my_ip/node/upload

Fired XHR event: loadstart
Fired XHR event: readystatechange
Fired XHR event: error

XHR status: 0
XHR status text: 
Fired XHR event: loadend

Edit 2: I tried sending the POST request using postman, and it worked just fine. I could retrieve the header like normal.

Edit 3: I add the CORS like so:

app.post('/upload', function(req, res) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Email-To");

  console.log(req.headers("Email-To"))
  console.log(req.get("Email-To"))
});

Solution

  • Please use lower-case name of param when getting value from req.headers

    app.post('/upload', function(req, res) {
         console.log(req.headers["email-to"])) //email-to
    })