Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response headers doesn't work #407

Open
joain946 opened this issue Nov 10, 2020 · 13 comments
Open

Response headers doesn't work #407

joain946 opened this issue Nov 10, 2020 · 13 comments

Comments

@joain946
Copy link

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
Copy link
Member

Please provide a minimum reproduction repository.

@joain946
Copy link
Author

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

@joain946
Copy link
Author

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

@nlaurie
Copy link

nlaurie commented Feb 3, 2021

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
Copy link

nlaurie commented Feb 3, 2021

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
Copy link

gristoi commented Mar 10, 2021

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
Copy link

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
Copy link

kniwase commented May 27, 2021

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
Copy link

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

@KaduMoura
Copy link

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
Copy link

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
Copy link

Hello everyone,

I have the same problem, will it be resolved?

@qstyler
Copy link

qstyler commented Nov 14, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests