0
votes

I have the following function which adds an item to a queue. Is there anyway of returning other status codes then 500 and 200? I have a case where I check if the caller has access to the endpoint & want to return a 403 if they don't.

[FunctionName("AddToQueue")]
[return: Queue("queue")]
public async Task<PushRequestMessage> AddToQueue(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = "")] HttpRequest req,
    ILogger log)
{
    if (!_accessRights.HasInternalAccess(req))
    {
        // return 403
    }

    return new Message()
    {
        Title = "Hello"  
    };
}
2
You need to change the return type. - Cindy Pau

2 Answers

1
votes

This can return an HttpResponseMessage like this:

[FunctionName("AddToQueue")]
[return: Queue("queue")]
public async Task<HttpResponseMessage> AddToQueue(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = "")] HttpRequest req,
    ILogger log)
{
    if (!_accessRights.HasInternalAccess(req))
    {
        //return 403
        return new HttpResponseMessage(HttpStatusCode.Forbidden);
    }

    return new Message()
    {
        Title = "Hello"  
    };
}
1
votes

You can throw an HttpResponseException like this:

[FunctionName("AddToQueue")]
[return: Queue("queue")]
public async Task<PushRequestMessage> AddToQueue(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = "")] HttpRequest req,
    ILogger log)
{
    if (!_accessRights.HasInternalAccess(req))
    {
        throw new HttpResponseException(HttpStatusCode.Forbidden);
    }

    return new Message()
    {
        Title = "Hello"  
    };
}