[英]How do I convert this CURL request to a normal HTTP request Javascript
我想使用從 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"
謝謝!
你需要澄清你所說的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 使用 ibm-watson sdk - https://github.com/watson-developer-cloud/node-sdk/tree/master/examples/browserify
或網絡包 - https://github.com/watson-developer-cloud/node-sdk/tree/master/examples/webpack
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.