0

the function looks like this:

import requests
import json
def parse(s):
    r = requests.post('http://166.111.139.15:9000/?properties%3d%7b%22annotators%22%3a%22tokenize%2cssplit%2cpos%2clemma%2cparse%22%2c%22outputFormat%22%3a%22json%22%7d%0a', data=s)
    return r.json()

print parse("I am a student")

And when I use it in Django, the web page shows: "No JSON object could be decoded".Why?

1
  • Try using return r.content() Commented Mar 12, 2016 at 7:35

1 Answer 1

3

No JSON object could be decoded is an exception message raised at r.json(). If your response is not a valid json object you can still retrieve it with r.text. Even if you are sure your response is always a valid json object, you should still check whether server returned a success code. If there's an internal server error (code 500), you won't get a valid json response!

import requests

def parse(s)
    r = requests.post('http://someserver.com', data=s)
    if r.status_code !== 200:
        return "There was a problem: {} !".format(r.text)
    return r.json()
Sign up to request clarification or add additional context in comments.

1 Comment

I like to use r.raise_for_status()

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.