如何修复来自Microsoft Azure计算机视觉服务的400响应传入

问题描述 投票:0回答:1

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

如何解决此问题?

我正在使用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。
  • 清理温度和预取文件。
c# azure computer-vision azure-cognitive-services bad-request
1个回答
0
投票
您可以使用Rapid API网站上的代码示例。

但是,在使用RestClient时,您不应该对urlpath进行urlencode。 Rapid API工程师可能在这里犯了一个错误。在大多数情况下,不需要对,字符进行urlencode。因此,您可以直接使用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中的代码示例:enter image description here

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

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()
{
    // With 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);
}

以及成功的结果:

enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.