英文:
Azure HTTP Triggere Function Empty Body
问题
我有两个在Azure函数应用上运行的函数。其中一个函数是由Cosmos DB触发的,调用另一个由HTTP触发的函数。在本地测试时,一切都按预期运行,但在发布时,似乎无法正确传递给HTTP触发的函数。
HTTP函数:
try:
req_body = json.loads(req.get_json())
logger.info(req_body)
run_process(req_body)
return func.HttpResponse(status_code=200)
except OSError as exception:
logger.error(
f"在处理请求时发生错误: {exception}")
return func.HttpResponse(exception, status_code=400)
CosmoDB触发的函数:
def post_document(document: dict):
# 将文档序列化为JSON字符串
json_data = json.dumps(document, default=str)
logger.info(f"发布文档: {json_data}")
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", URL, headers=headers, json=json_data, timeout=10)
assert response.status_code == 200, f'ER POST失败 {response.content}'
logger.info(f'ER POST请求成功')
奇怪的是,当我硬编码数据时,它似乎运行正常。当我复制并粘贴由这一行返回的数据时 "logger.info(f"posting document: {json_data}")" 并硬编码它时,它也能正常工作。你知道可能是什么原因吗?
英文:
I have 2 functions running on my azure function app. One of the functions is cosmodb triggered and calls another function which is http triggered. When testing locally everything works as expected but when publishing the body does not seem to pass to the HTTP triggered function correctly.
HTTP Function:
try:
req_body = json.loads(req.get_json())
logger.info(req_body)
run_process(req_body)
return func.HttpResponse(status_code=200)
except OSError as exception:
logger.error(
f"An error occurred while processing the request: {exception}")
return func.HttpResponse(exception, status_code=400)
CosmoDB Triggered Function:
def post_document(document: dict):
# Serialize the document to a JSON string
json_data = json.dumps(document, default=str)
logger.info(f"posting document: {json_data}")
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", URL, headers=headers, json=json_data, timeout=10)
assert response.status_code == 200, f'ER POST failed {response.content}'
logger.info(f'ER POST request successful')
The weird thing is that when I hardcode the data it seems to work fine, when I copy and paste the data returned by this line "logger.info(f"posting document: {json_data}")" and hard code that it works as well. Any ideas why this might be the case?
答案1
得分: 0
在我这一端复制后,按照以下步骤,我得到了期望的结果。
init .py
import logging
import azure.functions as func
import logger
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP触发函数处理了一个请求。')
try:
req_body = req.get_json()
logger.logger.info(req_body)
return func.HttpResponse(status_code=200)
except OSError as exception:
logger.error(f"在处理请求时发生错误:{exception}")
return func.HttpResponse(exception, status_code=400)
requirements.txt
azure-functions==1.13.3
logger==1.4
由于http函数接收到JSON数据,我将req_body = json.loads(req.get_json())
替换为req_body = req.get_json()
。
本地运行函数时的结果
部署函数时的结果
英文:
After reproducing from my end, I got the desired results after following the below.
init .py
import logging
import azure.functions as func
import logger
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
try:
req_body = req.get_json()
logger.logger.info(req_body)
return func.HttpResponse(status_code=200)
except OSError as exception:
logger.error(
f"An error occurred while processing the request: {exception}")
return func.HttpResponse(exception, status_code=400)
requirements.txt
azure-functions==1.13.3
logger==1.4
Since JSON data is received to http function I've replace req_body = json.loads(req.get_json())
with req_body = req.get_json()
.
Results when the function runs locally
Results when the function is deployed
答案2
得分: 0
我遇到了相同的问题。对我而言,使用特定的 azure-functions 版本并没有解决问题。对我而言,解决问题的方法是在函数网址中使用 https 而不是 http。
英文:
I had the same issue. For me using the specific azure-functions version did not solve the issue. For me what solved the issue is using https instead of http in the function url.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论