在 python 中发出 Azure Functions POST 请求
Make an Azure Function POST request in python
我目前正在学习 Azure Functions,并且在 Python 中有一些测试脚本。
在该函数中,我希望能够执行 post 请求以从其他地方获取一些数据,但是我无法像通常在 python 中那样导入请求库。
知道我应该在哪里查看或我可以在 Azure 函数中使用的其他库吗?
我可以在使用 VS 代码发布时重现您的问题,主要是将模块放在 requirements.txt 中,它会自动安装模块,但是它会被 VS 代码忽略,假设您使用的是 visual studio 代码也是。
如果是,您可以尝试使用 Azure Functions Core Tools
、Remote build
和 Local build
它们都可以使用模块成功发布函数。
更多细节你可以参考文档:Publishing to Azure.
试试 func azure functionapp publish <APP_NAME> --build local
。
您可以像这样使用来自 python 的请求的 api:
import requests
import base64
pat = 'token'
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii') #you can use this function to encode the token to base64 or directly encode the code to base64 and after that, use token with authorization variable
headers = {
'Authorization': 'Basic '+authorization,
'Content-Type': 'application/json-patch+json'
}
data = [
{
"op": "add",
"path": "/fields/System.Title",
"from": 'null',
"value": "Test"
}
]
response = requests.post(
url="https://dev.azure.com/{project}/{team}/_apis/wit/workitems/$task?api-version=6.0-preview.3", json =data, headers=headers)
print(response.json())
#this example you can create a workitem in task, for example
我目前正在学习 Azure Functions,并且在 Python 中有一些测试脚本。
在该函数中,我希望能够执行 post 请求以从其他地方获取一些数据,但是我无法像通常在 python 中那样导入请求库。
知道我应该在哪里查看或我可以在 Azure 函数中使用的其他库吗?
我可以在使用 VS 代码发布时重现您的问题,主要是将模块放在 requirements.txt 中,它会自动安装模块,但是它会被 VS 代码忽略,假设您使用的是 visual studio 代码也是。
如果是,您可以尝试使用 Azure Functions Core Tools
、Remote build
和 Local build
它们都可以使用模块成功发布函数。
更多细节你可以参考文档:Publishing to Azure.
试试 func azure functionapp publish <APP_NAME> --build local
。
您可以像这样使用来自 python 的请求的 api:
import requests
import base64
pat = 'token'
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii') #you can use this function to encode the token to base64 or directly encode the code to base64 and after that, use token with authorization variable
headers = {
'Authorization': 'Basic '+authorization,
'Content-Type': 'application/json-patch+json'
}
data = [
{
"op": "add",
"path": "/fields/System.Title",
"from": 'null',
"value": "Test"
}
]
response = requests.post(
url="https://dev.azure.com/{project}/{team}/_apis/wit/workitems/$task?api-version=6.0-preview.3", json =data, headers=headers)
print(response.json())
#this example you can create a workitem in task, for example