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

Params are missing when using http streams #229

Closed
ejizba opened this issue Feb 27, 2024 · 0 comments · Fixed by #238
Closed

Params are missing when using http streams #229

ejizba opened this issue Feb 27, 2024 · 0 comments · Fixed by #238
Labels
bug Something isn't working P1
Milestone

Comments

@ejizba
Copy link
Contributor

ejizba commented Feb 27, 2024

When using http streams, the request.params object will be an empty object. The suggested workaround is to use a package like path-to-regexp to parse the request.url property. Keep in mind that Azure Functions only supports ASP.NET Core route templates (i.e. user/{name}) when registering your function, so you will likely need to use a different syntax (i.e. /user/:name) when parsing the url. Here is example code using the suggested workaround:

import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions';
import { match } from 'path-to-regexp';

export async function httpTrigger1(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
    const parseRoute = match('/api/user/:name');
    const route = parseRoute(new URL(request.url).pathname);
    const params = route ? (route.params as Record<string, string>) : {};

    const name = params.name || 'world';

    return { body: `Hello, ${name}!` };
}

app.http('httpTrigger1', {
    methods: ['GET', 'POST'],
    route: 'user/{name}',
    authLevel: 'anonymous',
    handler: httpTrigger1,
});

Related to Azure/azure-functions-host#9840

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant