0

My Flask web service has this code:

@app.route('/api/my_service', methods=['GET', 'POST'])
def my_service():
    try:
        content = request.get_json()
        print type(content)
        print 'content', content
    except:
        print sys.exc_info()

My client calls this:

input = {'my_key': 1, 'other_key': 2}
url ='http://local:5000/api/my_service'
res = requests.post(url, json=input)
if res.ok:
    print res.text

The output is this
content {u'my_key': 1, u'other_key': 2}

It looks like my dictionary is being converted incorrectly. How can I trouble-shoot this?

Jeanne Lane
  • 495
  • 1
  • 9
  • 26
  • You might want to look at this: https://stackoverflow.com/questions/20001229/how-to-get-posted-json-in-flask – Tony Mar 01 '19 at 20:44
  • Possible duplicate of [How to get POSTed json in Flask?](https://stackoverflow.com/questions/20001229/how-to-get-posted-json-in-flask) – Tony Mar 01 '19 at 20:47
  • Those questions don't address why the dictionary is coming up in unicode. – Jeanne Lane Mar 01 '19 at 21:11
  • 1
    According to https://stackoverflow.com/a/11435633/8916412 **"The requests docs say that request will always return unicode, and the example content you posted is in fact unicode (notice the u'' string syntax? That's Python's syntax for unicode strings.), so there's no problem. Note that if you view the JSON response in a web browser, the u'' will not be there because it's a property of how Python stores a string."** – bc291 Mar 01 '19 at 22:38
  • 2
    Although, by switching to Python 3, you will get rid of that. – bc291 Mar 01 '19 at 22:44

0 Answers0