Home > front end >  Receiving TypeError in Azure Function with Python runtime
Receiving TypeError in Azure Function with Python runtime

Time:05-30

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

CodePudding user response:

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
        )
  • Related