gpt4 book ai didi

c# - Azure 机器学习 - 文本分析 C# 错误请求正文,即使在验证 JSON 正文后也是如此

转载 作者:行者123 更新时间:2023-11-30 09:54:00 24 4
gpt4 key购买 nike

使用 Azure 机器学习 - 文本分析 REST API,位于此处。需要通过 POST 向服务器发送有效负载。我正在尝试获得与 IBM watson 类似的结果

这是我在控制台应用程序中尝试的内容,这是核心代码:

static IRestResponse GetResp(string url, string key, string jsonText) {
IRestClient client = new RestClient(url);
IRestRequest request = new RestRequest() { RequestFormat = DataFormat.Json };
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Ocp-Apim-Subscription-Key", key);
IRestResponse response = client.ExecuteAsPost(request, "POST");

}

//  Here the code that serializes the object to look precisely like body advertised calls it: 
string json = JsonConvert.SerializeObject(documents);
IRestResponse resp = GetResponse("https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases", TaxonomyGlueKey, json);

序列化“文档”的消息正文是:

{
"documents": [
{
"language": "en",
"id": "4",
"text": "Lateral internal sphincterotomy and fissurectomy"
},
{
"language": "en",
"id": "5",
"text": "Fissurectomy and Botox injection"
}
]}

我收到错误请求错误。我已验证我的请求已发送并通过身份验证(之前已失败)。我也尝试过很多变体。

我可以尝试我的请求正文,并且将文本从调试变量直接复制到 Azure 提供的正文时它可以正常工作:

https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0/operations/56f30ceeeda5650db055a3c6/console

如果我使用上述内容进行测试,我会得到预期的响应,状态 200:

Transfer-Encoding: chunked
x-aml-ta-request-id: c4ea9fff-8068-42a3-99c4-68717acddcf5
X-Content-Type-Options: nosniff
apim-request-id: e5eb593b-96a3-4806-9143-1d83424569be
Date: Thu, 21 Jul 2016 14:14:44 GMT
Content-Type: application/json; charset=utf-8

{
"documents": [
{
"keyPhrases": [
"fissurectomy"
],
"id": "4"
},
{
"keyPhrases": [
"Botox injection"
],
"id": "5"
}
],
"errors": []
}

最佳答案

我正在使用 JQueryREST API 进行情感分析。我收到了与您收到的相同的错误。我设法通过提供输入的 JSON 序列化版本 作为请求正文来使其正常工作。

这是工作代码-

$(function() {
var params ={
"documents": [
{
"language": "en",
"id": "1",
"text": "this is AWESOME!"
}
]
};


$.ajax({
url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment?" + $.param( params );,
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","<your subscription key here>");
xhrObj.setRequestHeader("Accept","application/json");
},
type: "POST",
// Request body
data: JSON.stringify(params)
})
.done(function(data) {
alert("Sentiment score is " + data.documents[0].score);

})
.fail(function() {
alert("error");
});
});

关于c# - Azure 机器学习 - 文本分析 C# 错误请求正文,即使在验证 JSON 正文后也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38507113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com