1

So I'm trying to use python requests to make a PUT request to Azure (to create/update notification hub - https://learn.microsoft.com/en-us/rest/api/notificationhubs/notificationhubs/createorupdate#mpnscredential .

My code:

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)

But running r gives me 415 error.

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

Where did that \'application/x-www-form-urlencoded\' come from?
I am explicitly setting headers for that request, and that one is not included... I am clueless.

I have tried the "Try" functionality on the afformentioned Azure page, where you can try constructing the body yourself, but it is buggy...

Thanks for any help!

PrakashG
  • 1,642
  • 5
  • 20
  • 30
DomPkc23
  • 37
  • 6
  • Side note, there is an SDK for that to avoid dealing with lowlevel layers: https://pypi.org/project/azure-mgmt-notificationhubs/ – Laurent Mazuel Jan 18 '19 at 02:44

1 Answers1

1

The HTTP header should Content-Type and not contentType.

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

Also, parameter data should be JSON encoded.

r = requests.put(url, json=data, headers=headers)
Tobey
  • 1,400
  • 1
  • 10
  • 25
laika
  • 1,319
  • 2
  • 10
  • 16
  • @Tobey Thank you, that covered the 415 error... I also had to use jsonpickle, bcs bytearray isnt json serializable... The error Im getting now is: >>> r.text u'{"error":{"code":"InvalidRequestContent","message":"The request content was invalid and could not be deserialized: \'Could not find member\'properties.apnsCredential\' on object of type \'ResourceDefinition\'. Path \'[\'properties.apnsCredential\']\', line 1, position 56.\'."}}' So I must be constructing the json in a bad manner... anyone knows where to look how to translate properties.admCredential to a json? – DomPkc23 Jan 20 '19 at 19:58
  • Well, I accepted your answer, @Tobey , bcs it answered my question. Im not able to get it running tho, I gave up on requests, bcs its impossible for me to get the bearer token (in the above example I used one from the testing site where you can try putting the request together), the documentation is confusing for me to say the least... Im now trying with the python library – DomPkc23 Jan 21 '19 at 16:25