通过Node请求向uClassify API发出请求
Make request to uClassify API via Node request
我正在尝试从Node构建一个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."}"
}"
文档中没有关于JS的明确说明,我想知道我在request
代码中是否正确地在cURL中实现了他们的示例。
url-X POST-H"授权:令牌YOUR_READ_API_KEY_HERE"-H"内容类型:application/json"--data"{''"texts''":[''"我很高兴今天''"]}"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);
}
});
相关文章:
- 在Hapi.js Restful API中了解请求IP
- DreamFactory REST API POST休息/用户/会话请求总是在IE9中返回错误
- 向Yahoo Finance API发出JSONP请求时获得400错误请求
- 使用用户名/API令牌javascript的Jenkins POST请求
- 请求拥有API终结点
- AWS API网关返回400错误请求,但Postman工作正常
- 获取请求,谷歌picasa API
- 是Google自定义搜索API的客户端请求数量有限
- 岗位http://localhost:3000/api/signup400(错误请求)
- Google Maps JavaScript API v3:getDetails(请求,回调),用于多个位置
- AJAX发布请求使用qwest.js到goo.gl url shortener api
- 如何使用Chrome扩展API获取网页的所有HTTP请求
- PHP中的Curl请求-使用API
- 取消FB.api(..)请求
- 跨节点服务器上的多个 API 请求维护数据
- 每次都必须按 CTRL+f5 才能点击 api.如果不按它,就会弹出跨源请求阻止错误
- 尝试跨站点 JSON 使用 JS(Angular)请求 API
- 在 foreach 中请求 API
- jquery ajax请求api时出现CORS错误
- Angular请求api和令牌