Search code examples
deno

Deno, why i cannot get custom header


enter image description here

In this picture, I have send Aaaa and MOck-Query-Path, But in m Deno, i cannot get them, why.

Deno.serve(async (req) => {
    console.log(1111,req.headers);

Solution

  • The Deno.serve request object has access to all headers sent in the request, including custom headers. Use the req.headers.get("header-name") function to get the value for a specific header (not case-sensitive).

    If you want an object with all of the headers, Object.fromEntries(req.headers.entries()) - just note the resulting object's keys will be all normalized to lower case values.

    Tested with Deno version 1.38.1

    Deno.serve(async (req) => {
      console.log(req.headers.get("Mock-Query-Path"));
      return new Response(
        JSON.stringify(Object.fromEntries(req.headers.entries())),
      );
    });
    

    Test using curl

    curl localhost:8000 \
      -H "Aaaa: 20231013-direct-mail" \
      -H "Mock-Query-Path: /customer/getCustomerDetails"
    

    Console Output:

    /customer/getCustomerDetails
    

    Response

    {
      "aaaa": "20231013-direct-mail",
      "accept": "*/*",
      "host": "localhost:8000",
      "mock-query-path": "/customer/getCustomerDetails",
      "user-agent": "curl/7.81.0"
    }