我可以先让你知道我对此很陌生,所以请原谅我的无知。
我想创建一个触发的天蓝色函数,通过现有的 API 使用数据。我编写的代码是用 Python 编写的。
我在下面的 Visual Studio 代码中创建了一个 http 触发的 azure 函数,
import logging
import azure.functions as func
import http.client
import json
import pandas as pd
import numpy as np
import pyodbc
from datetime import datetime
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name=='example':
# Extracting the dataset size for each resource
size_= 0
from_= 0
creationdatetime_= '2000-01-01'
conn = http.client.HTTPSConnection("*****.api.******.com") # This is the http that I want to consume the data from
payload = ''
headers = {
'Authorization': 'Basic **********',
'Cookie': '***************'
}
conn.request("GET", "/*****/******?size={}&from={}&creationdatetime={}".format(size_,from_,creationdatetime_), payload, headers)
res = conn.getresponse()
data = res.read()
#The_Resource=data.decode("utf-8")
#The_Resource=json.loads(data) # Converting JSON string to dictionary
#total = The_Resource['total']
return func.HttpResponse(data)
else:
return func.HttpResponse("Wrong 'name' key parameter entered.Please check the name and run again.",status_code=200)
当我使用本地机器通过 Postman 触发它并使用参数名称:例如,我在下面得到正确的响应,
{"size":"0","total":1182,"from":"0"}
但是当我使用相同的参数在邮递员上部署它并对其进行测试时,我得到状态 500 内部服务器错误。当我在 azure 门户上尝试时,我得到 200 OK 但输出如下,
<html style="height:100%"><head><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><meta
name="format-detection" content="telephone=no"><meta name="viewport" content="initial- scale=1.0"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><script type="text/javascript" src="/_Incapsula_Resource?SWJIYLWA=719d34d31c8e3a6e6fffd425f7e032f3"> </script></head><body style="margin:0px;height:100%"><iframe id="main-iframe" src="/_Incapsula_Resource?CWUDNSAI=23&xinfo=3-66669102- 0%200NNN%20RT%281627064616349%205%29%20q%280%20-1%20-1%200%29%20r%280%20- 1%29%20B16%20U5&incident_id=374000410210161318- 229498008368450115&edet=16&cinfo=ffffffff&rpinfo=0&mth=GET" frameborder=0 width="100%" height="100%" marginheight="0px" marginwidth="0px">Request unsuccessful. Incapsula incident ID: 374000410210161318-229498008368450115</iframe></body></html>