Why are headers completely missing from a request using javascript in Azure Functions?

Ryan Cammer 20 Reputation points
2023-10-09T03:10:32.6133333+00:00

I have a simple function that should return the headers, and the headers are empty no matter what I do or try. I've used ChatGPT and Google, and the former is out of ideas, and the latter didn't turn anything up.

Here's the json output:

{
    "headers": {}
}

Here's the function.json:

{  
  "bindings": [    
    {      
      "dataType": "binary",     
      "authLevel": "anonymous",      
      "type": "httpTrigger",      
      "direction": "in",      
      "name": "req",      
      "methods": [       
        "get"      
      ],      
      "route": "download"    
    },    
    {     
      "type": "http",      
      "direction": "out",      
      "name": "res"    
    }  
  ]
}

Here's the packages in package.json:

"dependencies": {  
  "@azure/functions": "^4.0.1",  
  "@azure/storage-blob": "^12.16.0"
},

Here's the code:

const { app } = require("@azure/functions");

async function download(request, context) {
  return { 
    status: 200, 
    jsonBody: {       
      headers: request.headers    
    }      
  };
}

app.http(
  'download',   
  { 
    route: "download",
    methods: ['GET'],       
    authLevel: 'anonymous',        
    handler: download  
  }
);

module.exports = download;
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,386 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pramod Valavala 20,591 Reputation points Microsoft Employee
    2023-10-09T19:21:39.21+00:00

    @Ryan Cammer The behavior you see if expected and is because the headers object is not a simple JS object.

    You would need to construct a JSON object from it and return that instead. This code should work for you

    const headers = Object.fromEntries(request.headers.entries());
    
    return {
        jsonBody: {
            headers
        }
    };
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful