【问题标题】:How to fix 400 Response Incoming from Microsoft Azure Computer Vision service如何修复来自 Microsoft Azure 计算机视觉服务的 400 响应传入
【发布时间】:2020-05-24 08:36:35
【问题描述】:

我在 RapidAPI 上订阅了Computer Vision API。当我在 RapidAPI 平台上测试 API 时,它运行良好。但是当我从我的应用程序中调用它时,它会响应 400 Bad request。

我该如何解决这个问题?

我正在使用 RESTSharp 库。

这是我的代码 -

public static IRestResponse GetClassifiedImageData()
{
    var client = new RestClient("{Client Path}");
    var request = new RestRequest(Method.POST);
    request.AddHeader("x-rapidapi-host", "{Rapid API host}");
    request.AddHeader("x-rapidapi-key", "{My API key}");
    request.AddHeader("content-type", "application/json");
    request.AddHeader("accept", "application/json");
    request.AddParameter("application/json", "{\"url\":\"Image URL\"}", ParameterType.RequestBody);
    return client.Execute(request);
}

如果我异步调用,我会收到这条消息 -

System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[System.String,ComputerVision.Program+d__2]

异步代码-

public static async Task<IRestResponse> GetClassifiedImageData2()
{
    var client = new RestClient("{Client Path}");
    var request = new RestRequest(Method.POST);
    request.AddHeader("x-rapidapi-host", "{Rapid API host}");
    request.AddHeader("x-rapidapi-key", "{My API key}");
    request.AddHeader("content-type", "application/json");
    request.AddHeader("accept", "application/json");
    request.AddParameter("application/json", "{\"url\":\"Image URL\"}", ParameterType.RequestBody);
    return await client.ExecuteAsync(request);
}

这些我都试过了-

  • 重新启动 Visual Studio。
  • 清理临时文件和预取文件。

【问题讨论】:

  • 您的标题错误。使用像 wireshark 或 fiddler 这样的嗅探器,并将 api 请求与您的请求进行比较。然后修改您的标头以匹配 api 标头。
  • 您必须查看请求而不是响应。糟糕的响应是由于请求造成的。看起来 Postman 和 c# 中“代码”请求中的标头不同。
  • 我什么都不懂。你所说的一切都超出了我的想象。 :(你能给我一些资源来理解吗?或者你能详细说明一下吗?
  • 大家都用过嗅探器吗?您可以从网上免费下载 wireshark 或 fiddler。嗅探器将提供通过 Internet 发送的实际消息,以便您查看实际发送和接收的消息。
  • 嗨!我安装了提琴手,这是一个快照,我什么都不懂。 ????????? imgur.com/a/pJqUxAa

标签: c# azure computer-vision azure-cognitive-services bad-request


【解决方案1】:

根据我的测试,是 URL 编码导致了问题。

您可以使用 Rapid API 网站上的代码示例。 但是,在使用RestClient 时,您不应该对 urlpath 进行 urlencode。 Rapid API 工程师在这里可能会犯错误。在大多数情况下,, 字符不需要进行 urlencoded。因此,您可以直接使用https://microsoft-azure-microsoft-computer-vision-v1.p.rapidapi.com/analyze?visualfeatures=Categories,Tags,Color,Faces,Description 作为路径字符串。

而且,无论如何,正确的编码字符串是https://microsoft-azure-microsoft-computer-vision-v1.p.rapidapi.com/analyze?visualfeatures=Categories%2cTags%2cColor%2cFaces%2cDescription

Rapid API 的代码示例:

我使用了示例,得到的错误信息与您的相同。但我通过使用原始字符串或正确的编码字符串解决了它:

public static async Task<IRestResponse> GetClassifiedImageDataAsync()
{
    // The correct encoded string will work
    //var client = new RestClient("https://microsoft-azure-microsoft-computer-vision-v1.p.rapidapi.com/analyze?visualfeatures=Categories%2cTags%2cColor%2cFaces%2cDescription");

    // The original string will work 
    var client = new RestClient("https://microsoft-azure-microsoft-computer-vision-v1.p.rapidapi.com/analyze?visualfeatures=Categories,Tags,Color,Faces,Description");

    var request = new RestRequest(Method.POST);
    request.AddHeader("x-rapidapi-host", "microsoft-azure-microsoft-computer-vision-v1.p.rapidapi.com");
    request.AddHeader("x-rapidapi-key", "71a69********************************3ddb");
    request.AddHeader("content-type", "application/json");
    request.AddHeader("accept", "application/json");
    request.AddParameter("application/json", "{\"url\":\"https://upload.wikimedia.org/wikipedia/commons/1/11/Kanye_West_at_the_2009_Tribeca_Film_Festival.jpg\"}", ParameterType.RequestBody);
    return await client.ExecuteAsync(request);
}

static void Main(string[] args)
{
    var result = GetClassifiedImageDataAsync().GetAwaiter().GetResult();
    Console.WriteLine(result.Content);
}


您也可以使用RestClient的方法添加查询字符串:

public static async Task<IRestResponse> GetClassifiedImageDataAsync()
{
    // Without query string
    var client = new RestClient("https://microsoft-azure-microsoft-computer-vision-v1.p.rapidapi.com/analyze");

    var request = new RestRequest(Method.POST);

    // Add as query string manually
    request.AddParameter("visualfeatures", "Categories,Tags,Color,Faces,Description", ParameterType.QueryString);

    request.AddHeader("x-rapidapi-host", "microsoft-azure-microsoft-computer-vision-v1.p.rapidapi.com");
    request.AddHeader("x-rapidapi-key", "71a69********************************3ddb");
    request.AddHeader("content-type", "application/json");
    request.AddHeader("accept", "application/json");
    request.AddParameter("application/json", "{\"url\":\"https://upload.wikimedia.org/wikipedia/commons/1/11/Kanye_West_at_the_2009_Tribeca_Film_Festival.jpg\"}", ParameterType.RequestBody);
    return await client.ExecuteAsync(request);
}

以及成功的结果:

【讨论】:

  • , 将被编码为%2c,但是,Repaid API 工程师似乎再次编码了% 字符。 % 将被编码为 %25。所以,你得到了奇怪的字符串%252c
  • @MahmudulHasan 你试过我的解决方案了吗?有用吗?
猜你喜欢
  • 2023-03-23
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 2019-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多