Michael Ghorb
Michael Ghorb

Reputation: 321

azure functions return http response from a custom util function

Suppose I have an API for signup. I want to get data from the request and validate it in a Util function that I wrote and placed in another directory (see signup API and my Util directory in the picture). If validation goes fail, I want to return the HTTP response directly from that Util function. How can I achieve this?

my project directory

In the init.py

from utils.common_utils import validate
def main(req: func.HttpRequest) -> func.HttpResponse:
    req_body = validate(req)
    
    # other stuff

    return func.HttpResponse("ok", status_code=200)

and in my util function (validate function)

def validate(input):
    if ...:
      return func.HttpResponse("check your input", status_code=406)
       

Upvotes: 1

Views: 1320

Answers (1)

Hury Shen
Hury Shen

Reputation: 15754

As far as I know, I don't think it can return HttpResponse directly from that Util function and exit the main function. In your code, you can just return HttpResponse to req_body and then execute the following code of main function.

But I think if you write the code like below, it can also meet your requirement. It's the same thing.

def main(req: func.HttpRequest) -> func.HttpResponse:

    req_body = validate(req)

    if req_body.status_code == 406:
        return req_body

    return func.HttpResponse("ok", status_code=200)

Upvotes: 1

Related Questions