pythonapikommunicate

405 Error when trying to use the Send Message API endpoint


I'm using Python's request package with the following code:

APIKEY = "XXX"
url = 'https://services.kommunicate.io/rest/ws/message/v2/send HTTP/1.1'

myobj = {
    'groupId': "xxx",
    'message':'Hello',
    "fromUserName":'yyy'
        }

headers = {
    'Api-Key':APIKEY,
}
response = requests.post(url, data = myobj,headers=headers)

And is giving me the following error:

'{"status":"error","errorResponse":[{"errorCode":"AL-MA-01","description":"method not allowed","displayMessage":"Request method \\u0027POST\\u0027 not supported"}],"generatedAt":1591385905404}'

What am I doing wrong?


Solution

  • There are few issues with the code.
    1. HTTP/1.1 is not part of the URL.
    2. In requests package, in order to pass a JSON to server, there are multiple ways to do.

    a. Use json parameter provided in requests.post method for sending JSON data to the server, something like below code:

    import requests
    
    APIKEY = "XXX"
    url = 'https://services.kommunicate.io/rest/ws/message/v2/send'
    myobj = {
        'groupId': "xxx",
        'message': 'Hello',
        "fromUserName": 'yyy'
    }
    
    headers = {'Api-Key': APIKEY}
    
    response = requests.post(url, json=myobj, headers=headers)
    
    

    b. In Headers add "Content-Type": "application/json" and then first dump json data to string and then send it to server.

    import requests
    import json 
    
    APIKEY = "XXX"
    url = 'https://services.kommunicate.io/rest/ws/message/v2/send'
    myobj = {
        'groupId': "xxx",
        'message': 'Hello',
        "fromUserName": 'yyy'
    }
    
    headers = {
        'Api-Key': APIKEY,
        "Content-Type": "application/json"
    }
    
    myobj = json.dumps(myobj)
    
    response = requests.post(url, data=myobj, headers=headers)
    
    

    Also, Do check Difference between data and json parameters in python requests package