6

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

2

3 Answers 3

7

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
0

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.

1 Comment

Thanks for answering, I just had a loggin.info adding a try, except, else worked 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.