1

I'm having trouble understanding the problem here.

  • HTTP-triggered Azure Function
  • Python runtime
  • Testing on localhost with HTTPS (no problem here)
  • URL:https://localhost:5007/api/BARCODE_API
  • Goal: Check and validate the type parameter of the URL
    • Ensure it is present, a string, etc.

This works:

import azure.functions as func
import logging

def main(req: func.HttpRequest) -> func.HttpResponse:
    
    if req.params.get('type'):
        return func.HttpResponse(
            "Test SUCCESS",
            status_code=200
        )
    else:
        return func.HttpResponse(
            "Test FAIL",
            status_code=400
        )

I can't see why this does NOT work...

import azure.functions as func
import logging

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

    def check_type():
        try:
            if req.params.get('type'):
                return func.HttpResponse(
                    "Test SUCCESS",
                    status_code=200
                )
        except:
            return func.HttpResponse(
                "Test FAIL",
                status_code=400
            )

    check_barcode = check_type()
  • I also tried passing req.params.get('type') to the check_type() function, but same error..

Error

Exception: TypeError: unable to encode outgoing TypedData: unsupported type "<class 'azure.functions.http.HttpResponseConverter'>" for Python type "NoneType"

I can't see why this is happening when I send https://localhost:5007/api/BARCODE_API?type=ean13


EDIT 1: Using @MohitC's recommended syntax still causes the error above.

  • Test1 shows it failing with a Status 500 (crux of this question)
  • Test2 shows it succeeding with a Status 200 then failing with a Status 400 (as it should) enter image description here

1 Answer 1

1

The problem with your code is that if your if req.params.get('type'): is evaluating to false, then no exception is raised and your function returns None type, which is probably further causing the mentioned error.

There are couple of things you could do here,

  1. Return the test fail status code in the else part of your code.
  2. Raise an exception if the condition is not true, then the except part would return what you want.
    def check_type():
        try:
            if req.params.get('type'):
                return func.HttpResponse(
                    "Test SUCCESS",
                    status_code=200
                )
            else:
                return func.HttpResponse(
                    "Test FAIL",
                    status_code=400
                )
        except:
            return func.HttpResponse(
                "Test FAIL",
                status_code=400
            )

EDIT:

Based on architecture of your Azure API shown elegantly in Gif's, it looks like your main function must return something. You are collecting the HTTP response in check_barcode but not returning it.

Try below code:

import azure.functions as func
import logging

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

    def check_type():
        try:
            if req.params.get('type'):
                return True
            else:
                return False
        except:
            return False

    check_barcode = check_type()
    if check_barcode:
        return func.HttpResponse(
            "Test SUCCESS",
            status_code=200
        )
    else:
        return func.HttpResponse(
            "Test FAIL",
            status_code=400
        )
Sign up to request clarification or add additional context in comments.

5 Comments

The weird thing is that I am sending the type parameter and the if is still evaluated to None. I sent https://localhost:5007/api/BARCODE_API?type=ean13 to both examples in the original post. The first works, the second does not.
I captured this in EDIT 1 above.
I added some logging and the try: if block is evaluating to ean13 (AKA: Python is reading the type parameter from the URL). The failure happens on the return func.HttpResponse("Test SUCCESS", status_code=200). This seems like a bug to me.
@SeaDude updated with another answer, try it
Thanks for the advice. Your answer worked well for me.

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.