Search code examples
pythonsendbird

SendBird Error 400403 Invalid value: JSON body


I am trying to create user using send bird API
I am using python to make the API call, but getting error code 400403,
If I try from postman it works, not sure where I'm going wrong in the code

Following is my code -

import json
import requests

url = ‘https://api-someappidhere.sendbird.com/v3/users’

headers = {
    'Content-Type' : 'application/json; charset=utf8',
    'Api-Token' : 'secondaryApiToken'
}

data = {
    'user_id' : 'someuserId',
    'nickname' : 'somenickname',
    'profile_url' : ''
}

try:
    apiResponse = requests.post(url, headers=headers, data=data)
    apiResponse = apiResponse.json()
    
    return response

except Exception as error:
    print(error)

I am getting the following response -

{
    "hasError": false,
    "result": {
        "message": "Invalid value: \"JSON body.\".",
        "code": 400403,
        "error": true
    }
}

References -
https://docs.sendbird.com/platform/quick_start
https://docs.sendbird.com/platform/error_codes


Solution

  • You should use json.dumps().

    import json
    import requests
    
    url = ‘https://api-someappidhere.sendbird.com/v3/users’
    
    headers = {
        'Content-Type' : 'application/json; charset=utf8',
        'Api-Token' : 'secondaryApiToken'
    }
    
    data = {
        'user_id' : 'someuserId',
        'nickname' : 'somenickname',
        'profile_url' : ''
    }
    
    jsonData = json.dumps(data)
    
    try:
        apiResponse = requests.post(url, headers=headers, data=jsonData)
        apiResponse = apiResponse.json()
        
        return response
    
    except Exception as error:
        print(error)