【问题标题】:POST request to Watson API doesn't give a response对 Watson API 的 POST 请求没有给出响应
【发布时间】:2015-01-31 23:05:13
【问题描述】:

我正在尝试向 Watson API 发出发布请求,以使用他们的用户建模。我有以下代码:

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

我得到以下信息:

415 {"user_message":"HTTP 错误 415","error_code":"EUSERMOD00000"}

为什么会这样,我该如何解决?

【问题讨论】:

    标签: python http-post httprequest httpresponse ibm-watson


    【解决方案1】:

    来自requests API documentation

    data –(可选)要发送的字典、字节或类似文件的对象 请求的正文。

    您的input_data 是一个集合,它不是字典、字节或类似文件的对象。由于您的集合只有一个项目,您可能希望它是字节,您可以这样做:

    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."
    

    请注意,在您的原始代码中,每行末尾的反斜杠是不必要的,但现在需要 。您可以通过将整个内容包裹在一组括号中来使它们再次变得不必要,这可能是您最初的意图:

    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.")
    

    【讨论】:

    • 我意识到我需要将它作为一个 json 对象放入我的 post 请求中,所以我只是更改了我的代码,但我遇到了另一个错误,你能看看吗?
    【解决方案2】:

    这是最终代码。我遇到的问题是由于 raw_data 的格式不正确,我在下面修复了。

    """
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多