Home > OS >  How to get ip address of azure functions
How to get ip address of azure functions

Time:01-06

How can i get the IP address of the azure function.

I would like to be able to send a request to the function and have the ip returned

I have tried this code, which hang and never completed / returned anything

import { AzureFunction, Context, HttpRequest } from "@azure/functions";

const httpTrigger: AzureFunction = function (context: Context, req: HttpRequest): void {
    console.log("HTTP trigger function processed a request.");

    const ip = req.headers["x-forwarded-for"] as string;

    context.res = {
        body: { ip },
        status: 200
    };
};

export default httpTrigger;

CodePudding user response:

As you're not using an async Azure function you need to call context.done() after setting context.res in order to return.

Microsoft does recommend you use async functions.

import { AzureFunction, Context, HttpRequest } from "@azure/functions";

const httpTrigger: AzureFunction = function (context: Context, req: HttpRequest): void {
    console.log("HTTP trigger function processed a request.");

    const ip = req.headers["x-forwarded-for"] as string;

    context.res = {
        body: { ip },
        status: 200
    };
    context.done();
};

export default httpTrigger;
  • Related