Fight Fire With Fire
Fight Fire With Fire

Reputation: 1686

Using variables with JSON with Python Requests

I'm trying to use requests to send JSON using variables to an API. When hard coded it works fine (I.E. "videos" : "30" , or "views" : "100") but now that I replaced it with variables the server responds with:

{u'message': u"'vars' parameter is not a valid JSON"}

Here is my code:

return requests.post(
    "https://api.website.com",
    auth=('api', 'XXXXXXXXXXXXX'),
    data={'subscribed': True,
          'address': email,
          'name': username,
          'description': profile,
          'vars': '{"logo" : logo , "status" : status , "videos": videos , "views": views , "likes": likes}'  })

Upvotes: 1

Views: 2483

Answers (1)

e4c5
e4c5

Reputation: 53734

Don't produce json manually. Use the built in json module.

import json
data={'subscribed': True,
      'address': email,
      'name': username,
      'description': profile,
      'vars': json.dumps({"logo" : logo , "status" : status , "videos": videos , "views": views , "likes": likes})  })

Upvotes: 4

Related Questions