我無法理解這里的問題。
- HTTP 觸發的 Azure 函式
- Python 運行時
- 使用 HTTPS 在 localhost 上進行測驗(這里沒問題)
- 網址:
https://localhost:5007/api/BARCODE_API - 目標:檢查并驗證
typeURL的引數- 確保它存在、字串等。
這有效:
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
)
我不明白為什么這不起作用......
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()
- 我也嘗試傳遞
req.params.get('type')給check_type()函式,但同樣的錯誤..
錯誤
Exception: TypeError: unable to encode outgoing TypedData: unsupported type "<class 'azure.functions.http.HttpResponseConverter'>" for Python type "NoneType"
我不明白為什么在我發送時會發生這種情況https://localhost:5007/api/BARCODE_API?type=ean13
編輯 1:使用@MohitC 推薦的語法仍然會導致上述錯誤。
- Test1 顯示它以狀態 500 失敗(這個問題的癥結所在)
- Test2 顯示它以狀態 200 成功,然后以狀態 400 失敗(應該如此)

uj5u.com熱心網友回復:
您的代碼的問題是,如果您if req.params.get('type'):的評估結果為 false,則不會引發例外并且您的函式回傳None型別,這可能會進一步導致上述錯誤。
你可以在這里做幾件事,
- 在代碼的 else 部分回傳測驗失敗狀態代碼。
- 如果條件不成立,則引發例外,然后該
except部分將回傳您想要的。
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
)
編輯:
根據 Gif 中優雅顯示的 Azure API 架構,您的主函式似乎必須回傳一些內容。您正在收集 HTTP 回應,check_barcode但沒有回傳它。
試試下面的代碼:
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
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/484912.html
上一篇:指向成員函式的指標有什么意義?
下一篇:找出所有能被5整除的奇數
