nestjs / azure-func-http

Azure Functions HTTP adapter for Nest framework (node.js) 🌥
https://nestjs.com/
MIT License
151 stars 42 forks source link

Response headers doesn't work #407

Open joain946 opened 3 years ago

joain946 commented 3 years ago

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.

kamilmysliwiec commented 3 years ago

Please provide a minimum reproduction repository.

joain946 commented 3 years ago

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

joain946 commented 3 years ago

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

nlaurie commented 3 years ago

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 commented 3 years ago

Interesting Note

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

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

gristoi commented 3 years ago

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

sportelli commented 3 years ago

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

kniwase commented 3 years ago

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);
}
OskarsPakers commented 2 years ago

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

KaduMoura commented 2 years ago

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.

tiagoboeing commented 2 years ago

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.

brunoog33 commented 2 years ago

Hello everyone,

I have the same problem, will it be resolved?

qstyler commented 11 months ago

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?