1

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}'  })
1
  • It's because you didn't really replace it with variables, they're still strings. the logo inside the string is just the character sequence 'logo' not a reference to your variable. Commented Jun 25, 2017 at 1:14

1 Answer 1

4

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})  })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.