azure-func-http icon indicating copy to clipboard operation
azure-func-http copied to clipboard

Response headers doesn't work

Open joain946 opened this issue 4 years ago • 13 comments

I'm submitting a...


[x] Bug report

Current behavior

Response headers are not sent back to the client. The example below returns this response:


HTTP/1.1 200 OK
Date: Tue, 10 Nov 2020 12:39:58 GMT
Server: Kestrel
Content-Length: 12

Hello World!

Expected behavior

All response headers should be returned to the client. From the example below the "Content-Type" and "MyHeader" header should have been returned from the REST api.

Minimal reproduction of the problem with instructions

Using this instruction: https://trilon.io/blog/deploy-nestjs-azure-functions Then adding two @Header() decorators to the api:


import { Controller, Get, Header } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  @Header("Content-Type", "text/plain")
  @Header("MyHeader", "MyHeaderValue")
  getHello(): string {
    return this.appService.getHello();
  }
}

Environment


Nest version: 7.4.4
 
For Tooling issues:
- Node version: v12.18.0
- Platform:  Windows

Workaround

It seems like the writeHead(...) method is never called. Triggering this method from IDE or through code (in an interceptor) solves the issue.

joain946 avatar Nov 10 '20 12:11 joain946

Please provide a minimum reproduction repository.

kamilmysliwiec avatar Nov 16 '20 10:11 kamilmysliwiec

Repository: https://github.com/joain946/nestjs-azure

joain946 avatar Nov 16 '20 14:11 joain946

Any update on this @kamilmysliwiec? Do you need any additional input from me?

joain946 avatar Dec 17 '20 14:12 joain946

I am having the exact same issue, none of the nestjs headers are being sent to the azure function only stock azure response headers. Ran the code from git and npm and having same issue.

image

Seems like the writeHead bound function is never getting called, breakpoint not getting hit in bound function. this.writeHead = this.writeHead.bind(this, context);

Info: azure functions v3

nlaurie avatar Feb 03 '21 15:02 nlaurie

Interesting Note

When using AzureHttpRouter const app = await NestFactory.create(AppModule, new AzureHttpRouter());

The some headers seem to be sent , most notably 'content-type'

nlaurie avatar Feb 03 '21 17:02 nlaurie

Ive just hit this exact same issue. Has there been any movement on this.? Only way i can currently work arouns it is remove my app.enableCors() ( as this conflicts with the adapter ) and override the HttpAdapters context

gristoi avatar Mar 10 '21 14:03 gristoi

Hello everyone

@nlaurie @gristoi Did you find a workaround ? @kamilmysliwiec How can we help debugging it?

We are stuck with the exact same issue.

Thank you

Felix

sportelli avatar Apr 21 '21 12:04 sportelli

Hello everyone,

I've found a simple workaround. Please try this.

Thank you.

import { Context, HttpRequest } from '@azure/functions';
import { AzureHttpAdapter } from '@nestjs/azure-func-http';
import { createApp } from '../src/main.azure';

function createPsuedoApp(createApp: () => Promise<any>): () => Promise<any> {
  return async (): Promise<any> => {
    const app = await createApp();
    const psuedoApp = {
      getHttpAdapter: () => {
        return {
          getInstance: () => {
            return (req: any, res: any) => {
              const done = req.context.done;
              req.context.done = (err?: string | Error, result?: any) => {
                res.writeHead();
                done(err, result);
              };
              app.getHttpAdapter().getInstance()(req, res);
            };
          },
        };
      },
    };
    return psuedoApp;
  };
}

export default function(context: Context, req: HttpRequest): void {
  AzureHttpAdapter.handle(createPsuedoApp(createApp), context, req);
}

kniwase avatar May 27 '21 11:05 kniwase

Would be nice to have a pull request to fix it within azure-func-http.

OskarsPakers avatar Dec 06 '21 11:12 OskarsPakers

We solved thia on host.json file..

{ "version": "2.0", "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[2.*, 3.0.0)" }, "functionTimeout": "01:00:00", "extensions": { "http": { "routePrefix": "api", "customHeaders": { "Content-Type": "application/json" } } } }

All we needed is json response header..

We are using microsoft docker's img to emulate in local environment.

KaduMoura avatar Feb 17 '22 20:02 KaduMoura

Hello everyone,

I've found a simple workaround. Please try this.

Thank you.

import { Context, HttpRequest } from '@azure/functions';
import { AzureHttpAdapter } from '@nestjs/azure-func-http';
import { createApp } from '../src/main.azure';

function createPsuedoApp(createApp: () => Promise<any>): () => Promise<any> {
  return async (): Promise<any> => {
    const app = await createApp();
    const psuedoApp = {
      getHttpAdapter: () => {
        return {
          getInstance: () => {
            return (req: any, res: any) => {
              const done = req.context.done;
              req.context.done = (err?: string | Error, result?: any) => {
                res.writeHead();
                done(err, result);
              };
              app.getHttpAdapter().getInstance()(req, res);
            };
          },
        };
      },
    };
    return psuedoApp;
  };
}

export default function(context: Context, req: HttpRequest): void {
  AzureHttpAdapter.handle(createPsuedoApp(createApp), context, req);
}

Only quoting to appoint the file where you need to edit. Add this script on main/index.ts.

tiagoboeing avatar Mar 21 '22 19:03 tiagoboeing

Hello everyone,

I have the same problem, will it be resolved?

brunoog33 avatar May 24 '22 15:05 brunoog33

Somehow this script solves the header problem, but misses the 302 status, making it 200. Any way to keep the status and have the Location header?

qstyler avatar Nov 14 '23 12:11 qstyler