简体   繁体   中英

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 (IE "videos" : "30" , or "views" : "100") but now that I replaced it with varibles 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}'  })

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})  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM