Reputation: 43
I'm trying to make a post request to for Watson API to use their User Modeling. I have the following code:
import requests
import keys
import json
url = "https://gateway.watsonplatform.net/systemu/service/api/v2/profile"
username = keys.username
password = keys.password
text = "Call me Ishmael. Some years ago-never mind how long precisely-having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off-then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me."
raw_data = {"contentItems":text}
input_data = json.dumps(raw_data)
response = requests.post(url, auth=(username, password), data=input_data)
print response.status_code
print response.text
I get the following:
415 {"user_message":"HTTP Error 415","error_code":"EUSERMOD00000"}
Why is that the case and how can I fix it?
Upvotes: 0
Views: 753
Reputation: 43
Here's the final code. The issue that I was having was due to incorrect formatting of raw_data, which I fixed below.
"""
The script takes data file (min 1000 words needed) and makes a POST request to Watson-IBM API to analyze the text for User modeling.
To run the file you need to have your own keys file in the directory above. To get username and password, you need to create an app in Bluemix and bind the service.
"""
import json
import requests
from .. import keys
url = "https://gateway.watsonplatform.net/systemu/service/api/v2/profile"
username = keys.username
password = keys.password
with open ("data.txt", "r") as myfile:
text=myfile.read().replace('\n', '')
raw_data = {
'contentItems' : [{
'userid' : username,
'id' : 'dummyUuid',
'sourceid' : 'freetext',
'contenttype' : 'text/plain',
'language' : 'en',
'content': text
}]
}
input_data = json.dumps(raw_data)
response = requests.post(url, auth=(username, password), headers = {'content-type': 'application/json'}, data=input_data)
# print response.status_code # needed for testing
print response.text
Upvotes: 1
Reputation: 35059
From the requests API documentation:
data – (optional) Dictionary, bytes, or file-like object to send in the body of the Request.
Your input_data
is a set, which is none of a dictionary, bytes or file-like object. Since your set only has a single item, you probably want it to be bytes which you can do like this:
input_data = b"Call me Ishmael. Some years ago-never mind how long precisely-"\
"having little or no money in my purse, and nothing particular to interest me "\
"on shore, I thought I would sail about a little and see the watery part of "\
"the world. It is a way I have of driving off the spleen and regulating the "\
"circulation. Whenever I find myself growing grim about the mouth; whenever it "\
"is a damp, drizzly November in my soul; whenever I find myself involuntarily "\
"pausing before coffin warehouses, and bringing up the rear of every funeral I "\
"meet; and especially whenever my hypos get such an upper hand of me, that it "\
"requires a strong moral principle to prevent me from deliberately stepping "\
"into the street, and methodically knocking people's hats off-then, I account "\
"it high time to get to sea as soon as I can."
Note that your backslashes at the end of each line are unnecessary in your original code, but would be necessary now. You can make them unnecessary again by wrapping the whole thing in a set of brackets, which may be what you intended originally:
input_data = (b"Call me Ishmael. Some years ago-never mind how long precisely-"
"having little or no money in my purse, and nothing particular to interest me "
"on shore, I thought I would sail about a little and see the watery part of "
"the world. It is a way I have of driving off the spleen and regulating the "
"circulation. Whenever I find myself growing grim about the mouth; whenever it "
"is a damp, drizzly November in my soul; whenever I find myself involuntarily "
"pausing before coffin warehouses, and bringing up the rear of every funeral I "
"meet; and especially whenever my hypos get such an upper hand of me, that it "
"requires a strong moral principle to prevent me from deliberately stepping "
"into the street, and methodically knocking people's hats off-then, I account "
"it high time to get to sea as soon as I can.")
Upvotes: 1