我想使用从IBM Natural Language API获得的此代码段,但以Javascript运行它。如何将其转换为普通的HTTP POST请求?只是一些重新排列语法的指针将是惊人的,我知道代码中有一些占位符会根据需要更改。
$ curl -X POST -u "apikey:{apikey}" \
--header "Content-Type: application/json" \
--data '{
"text": "I love apples! I do not like oranges.",
"features": {
"sentiment": {
"targets": [
"apples",
"oranges",
"broccoli"
]
},
"keywords": {
"emotion": true
}
}
}' \
"{url}/v1/analyze?version=2019-07-12"
谢谢!
答案 0 :(得分:0)
您需要阐明javascript的意思。如果它是node.js,则可以使用ibm-watson sdk-https://cloud.ibm.com/apidocs/natural-language-understanding?code=node#analyze。
链接上有示例代码,向您展示如何操作。
const NaturalLanguageUnderstandingV1 = require('ibm-watson/natural-language-understanding/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
const naturalLanguageUnderstanding = new NaturalLanguageUnderstandingV1({
version: '2020-08-01',
authenticator: new IamAuthenticator({
apikey: '{apikey}',
}),
serviceUrl: '{url}',
});
const analyzeParams = {
'text': 'I love apples! I do not like oranges.',
'features': {
"sentiment": {
"targets": [
"apples",
"oranges",
"broccoli"
]
},
'keywords': {
'emotion': true
}
}
};
naturalLanguageUnderstanding.analyze(analyzeParams)
.then(analysisResults => {
console.log(JSON.stringify(analysisResults, null, 2));
})
.catch(err => {
console.log('error:', err);
});
如果您是在浏览器中引用javascript,那么您使用的内容实际上取决于您使用的javascript框架。对于原始JavaScript,这将是xhttp
,但是大多数框架都会为您简化该过程。
一种替代方法是通过browserify-https://github.com/watson-developer-cloud/node-sdk/tree/master/examples/browserify
使用ibm-watson sdk或Web Pack-https://github.com/watson-developer-cloud/node-sdk/tree/master/examples/webpack