python请求:对azure的PUT请求失败,出现415

2024-10-02 08:20:10 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我尝试使用python请求向Azure发出PUT请求(创建/更新通知中心-https://docs.microsoft.com/en-us/rest/api/notificationhubs/notificationhubs/createorupdate#mpnscredential)。在

我的代码:

url = "https://management.azure.com/subscriptions/mysub/resourceGroups/Default-NotificationHubs-WestEurope/providers/Microsoft.NotificationHubs/namespaces/myNamespace/notificationHubs/notificationHubName?api-version=2016-03-01"

bearer_token = "my very long token"

headers = {
    "dataType": "json",
    "accept":"application/json",
    "contentType":"application/json",
    "Authorization": "Bearer " + bearer_token }

filepath = "/Users/..../pathTo.p12"
    with open(filepath) as fh:
        byte_array_p12 = fh.read()

data = {
    'location': "West Europe",
    'properties.apnsCredential': {
        'properties.apnsCertificate': byte_array_p12,
        'properties.certificateKey': "some nice pass"
    }  
}

r = requests.put(url, data, headers = headers)

但是运行r会给我415个错误。在

^{pr2}$

那是从哪里来的?
我正在为该请求显式设置标头,但不包括该标头。。。我是个笨蛋。在

我已经尝试了“尝试”的功能,在那里你可以尝试自己构建身体,但它是有缺陷的。。。在

谢谢你的帮助!在


Tags: httpscomtokenapijsonurlapplicationproperties
1条回答
网友
1楼 · 发布于 2024-10-02 08:20:10

HTTP头应该是^{},而不是{}。在

headers = {
    "dataType": "json",
    "accept":"application/json",
    "Content-Type":"application/json",
    "Authorization": "Bearer " + bearer_token }

另外,参数data应该是JSON编码的。在

^{pr2}$

相关问题 更多 >

    热门问题