如何修复来自 Microsoft Azure Computer Vision 服务的 400 Response Incoming

How to fix 400 Response Incoming from Microsoft Azure Computer Vision service

我在 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);
}

我试过这些 -

根据我的测试,导致问题的是 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);
}

成功的结果: