简体   繁体   中英

Azure python function Exception while executing function: Functions.HttpExample. System.Private.CoreLib: Result: Failure

I have a azure python function. I am trying to post data to an API endpoint through my function. Below is the code I have for the same,

import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    try:
        req_body = req.get_json()
        url = "https://example.com/msgs"
        payload= req.get_body()
        headers = {
        'Authorization': 'mytoken ',
        }
        response = func.HttpRequest(method="POST", url=url, headers=headers, body=payload,params=None,route_params=None) 
        return func.HttpResponse("", response)
    except :
        func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

Every time I invoke my function, I get below error,

Executed 'Functions.HttpExample' (Failed, Id=bc8a184c-7b20-4946-a92c-ed2afad66e56, Duration=17ms)
[2021-10-15T20:14:11.494Z] System.Private.CoreLib: Exception while executing function: Functions.HttpExample. System.Private.CoreLib: Result: Failure
Exception: TypeError: unable to encode outgoing TypedData: unsupported type "<class 'azure.functions.http.HttpResponseConverter'>" for Python type "NoneType"
Stack:   File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 427, in _handle__invocation_request
    return_value = bindings.to_outgoing_proto(
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\bindings\meta.py", line 116, in to_outgoing_proto
    datum = get_datum(binding, obj, pytype)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\bindings\meta.py", line 107, in get_datum
    raise TypeError(

Can anyone help me to fix this issue? or tell me a way to make a POST call from azure python function?

Thanks, Tintu

I had the same error, but my problem was different. I had commented out a block of code, which included the following line:

return func.HttpResponse(“Request processed successfully.”, status_code=200)

Commenting out this line meant that the function would end without an HTTP response, therefore returning None. That is where the “None” type in my error came from. I hope this helps somebody else!

I had the same problem!

I get this error:

Exception while executing function: Functions.DouMonitorHttpStart <--- Result: Failure Exception: TypeError: unable to encode outgoing TypedData: unsupported type "<class 'azure.functions.http.HttpResponseConverter'>" for Python type "dict" Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 427, in _handle__invocation_request return_value = bindings.to_outgoing_proto( File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/bindings/meta.py", line 116, in to_outgoing_proto datum = get_datum(binding, obj, pytype) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/bindings/meta.py", line 107, in get_datum raise TypeError(

But after a re-run, it eventually succeeds.

This happened with me with I had a try block and a finally block, without a except or a else block. I added an except and a else block and it worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM