Azure functions all working but POST request

53 Views Asked by At

I created an Azure functions application and created 4 functions in it. I read the documentation and used the exact given technique to connect with my SQL server database which is also hosted on Azure.

The 4 functions are for CRUD operations. Now my error is quite weird.

When I run application like after an hour and first send the POST request through POSTMAN, it works fine but then I send PATCH request to make changes in my database but the function sends 500 error.

Take the same case, now when I run application after an hour and run PATCH request first and then POST, PATCH works fine but POST starts sending 500 error.

I don't understand why am I getting this issue. It is also worth noting that when I run each of these functions (POST and PATCH) individually from the Azure console they provide to test functions, they both work fine there. I don't understand this behaviour.

My code is exactly like the documentation so I am certain that my code is not misbehaving. What I am assuming is that maybe there is some issue with SQL Server connection pooling but I am not certain about it. I looked into SQL Server resources and they are barely touched so my resources are also not exhausted.

POST FUNCTION:

import {
    app,
    HttpRequest,
    HttpResponseInit,
    InvocationContext,
    output,
} from "@azure/functions";

const sql = output.sql({
    commandText: "Employees",
    connectionStringSetting: "SqlConnectionString",
});

export async function addEmployee(
    request: HttpRequest,
    context: InvocationContext
): Promise<HttpResponseInit> {
    try {
        const body = await request.json();
        context.extraOutputs.set(sql, body);

        return {
            jsonBody: { message: `Successfully added new Employee Record` },
        };
    } catch (e: Error | any) {
        return { jsonBody: e.message };
    }
}

app.http("addEmployee", {
    methods: ["POST"],
    authLevel: "anonymous",
    extraOutputs: [sql],
    handler: addEmployee,
    route: "employees/add",
});

PATCH FUNCTION

import {
    app,
    HttpRequest,
    HttpResponseInit,
    InvocationContext,
    output,
} from "@azure/functions";

const sql = output.sql({
    commandText: "Employees",
    connectionStringSetting: "SqlConnectionString",
});

export async function editEmployee(
    request: HttpRequest,
    context: InvocationContext
): Promise<HttpResponseInit> {
    try {
        const body = await request.json();
        context.extraOutputs.set(sql, body);

        return {
            jsonBody: { message: `Successfully edited new Employee Record` },
        };
    } catch (e: Error | any) {
        return { jsonBody: e.message };
    }
}

app.http("editEmployee", {
    methods: ["PATCH"],
    authLevel: "anonymous",
    extraOutputs: [sql],
    handler: editEmployee,
    route: "employees/edit",
});

0

There are 0 best solutions below