通过节点请求向uClassify API发出请求
问题描述:
我正在尝试从Node创建对uClassify API的请求。我无法弄清楚什么是错的,我写的代码:通过节点请求向uClassify API发出请求
const req = JSON.stringify('Hello, my love!');
const options = {
body: req,
method: 'POST',
url: 'https://api.uclassify.com/v1/uClassify/Sentiment/classify',
headers: {
'Content-Type': 'application/json',
Authorization: 'MyKey'
}
};
request(options, (error, response, body) => {
if (!error) {
callback(response);
}
});
我得到如下回应:
statusCode: 400,
body: "{"statusCode":400,
"message": "Error converting value \"Hello, my love!\" to
type 'UClassify.RestClient.TextPayload'. Path '', line 1, position 17."}"
}"
有一个在the documentation为JS没有明确的指示,我不知道是否我在我的request
代码中正确地在cURL中实现了他们的示例。
网址-X POST -H “授权:令牌YOUR_READ_API_KEY_HERE” -H “内容类型:应用程序/ JSON” --data “{\” 文本\ “:\” 我很高兴今天 “}}”https://api.uclassify.com/v1/uClassify/Sentiment/classify
答
在你的Node.js代码中你的身体不正确(但在你的cURL中你使用了正确的身体)。 uClassify期望具有属性texts
的对象。 更改您的node.js代码中的正文如下:
const req = JSON.stringify({ texts: ['Hello, my love!'] });
const options = {
body: req,
method: 'POST',
url: 'https://api.uclassify.com/v1/uClassify/Sentiment/classify',
headers: {
'Content-Type': 'application/json',
Authorization: 'MyKey'
}
};
request(options, (error, response, body) => {
if (!error) {
callback(response);
}
});
谢谢!我其实试图设置'texts'属性,但显然我搞砸了语法。还有一件事,我必须以'Authorization:Token MyKey'形式传递令牌。干杯! –