How to access http status in Azure Function v4

293 Views Asked by At

How do i read the http status of a http trigger function built using Azure Functions v4?

The equivalent in v3 would have been something like

const statusCode = context.res.status; context.log(HTTP Status Code: ${statusCode});

I've tried debugging and trying to find the status amongst the context variables but i understand it's not there. I can see from the documentation (https://learn.microsoft.com/en-us/azure/azure-functions/functions-node-upgrade-v4?tabs=v3) that you can set the status using a return status e.g. return { status: 200 }; but i don't know how to simply read the current status.

Thanks!

1

There are 1 best solutions below

3
On

How do i read the http status of a http trigger function built using Azure Functions v4?

I have created a Http triggered function.

code:

const { app } = require('@azure/functions');

app.http('httpTrigger1', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    handler: async (request, context) => {
        context.log(`Http function processed request for url "${request.url}"`);

        const name = request.query.get('name') || (await request.text()) || 'world';

        // Your response object
        const response = { body: `Hello, ${name}!` };

        // Log the HTTP status code
        context.log(`HTTP Status Code: ${response.status || 200}`); // Default to 200 if not set explicitly

        return response;
    }
});

The above code executed successfully. Output status in Local:

enter image description here

I have successfully deployed the http trigger function into Azure portal. check below:

enter image description here

Then click on the function, go to code+test, and run the code. It runs successfully, and the status is below: Output:

enter image description here