0

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"  
    };
}
1
  • You need to change the return type. Commented Feb 27, 2020 at 12:03

2 Answers 2

1

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"  
    };
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure this would ever work. HttpResponseMessage is very different from my own "Message" class. "Message" could be any POCO, and I don't see how I can return that through a HttpResponseMessage.
1

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"  
    };
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.