Python – How do I fix the null content error in the NLU Watson API?

How do I fix the null content error in the NLU Watson API?… here is a solution to the problem.

How do I fix the null content error in the NLU Watson API?

I’m streaming tweets in file tweet.txt and using another python script, I’m reading tweets and sending them to Watson. Sometimes, an error is generated:

Traceback (most recent call last): File “readingTweets.py”, line 44,
in
language=’en’ File “/usr/local/lib/python2.7/dist-packages/watson_developer_cloud/natural_language_understanding_v1.py”,
line 173, in analyze
method=’POST’, url=url, params=params, json=data, accept_json=True) File
“/usr/local/lib/python2.7/dist-packages/watson_developer_cloud/watson_service.py”,
line 385, in request
info=error_info, httpResponse=response) watson_developer_cloud.watson_service. WatsonApiException: Error:
invalid request: content is empty, Code: 400 , X-dp-watson-tran-id:
gateway02-582988317 , X-global-transaction-id:
ffea405d5adda40d22bfb21d

My code example is:

while 1:
    where = file.tell()
    line = file.readline()
    if not line:
        time.sleep(1)
        file.seek(where)
    else:
        if (line):
            print "-----------------------------"
            print "the line is: "
            print line
            print "-----------------------------"
            response = natural_language_understanding.analyze(
                text=line,
                features=Features(
                    entities=EntitiesOptions(
                        emotion=True,
                        sentiment=True,
                        limit=2),
                    keywords=KeywordsOptions(
                        emotion=True,
                        sentiment=True,
                        limit=2)),
                language='en'
                )
            print(json.dumps(response, indent=2))

Solution

This is the error: Invalid request: content is empty or invalid characters.
You suggested using characters like \n, and you only checked for null characters.

Related Problems and Solutions