1

所以我正在尝试使用 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 错误。

r.text
u'{"error":{"code":"UnsupportedMediaType","message":"The content media type \'application/x-www-form-urlencoded\' is not supported. Only \'application/json\' is supported."}}'

那是\'application/x-www-form-urlencoded\'从哪里来的?
我正在为该请求明确设置标头,并且不包括该标头……我一无所知。

我已经在上述 Azure 页面上尝试了“尝试”功能,您可以在其中尝试自己构建主体,但它有问题......

谢谢你的帮助!

4

1 回答 1

1

HTTP 标头应该Content-Type和不应该contentType

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

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

r = requests.put(url, json=data, headers=headers)
于 2019-01-17T13:20:34.413 回答