Search code examples
c#azurecomputer-visionazure-cognitive-servicesbad-request

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


I subscribed to Computer Vision API on RapidAPI. When I'm testing the API on the RapidAPI platform, it's working perfectly. But when I'm calling it from my app, it responding 400 Bad request.

How can I fix this problem?

I'm using the RESTSharp library.

Here's my code -

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

And if I call asyncronusly, I get this message -

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

Async Code-

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

I've tried these -

  • Restarting Visual Studio.
  • Cleaning Temp and prefetch files.

Solution

  • Based on my test, it is the URL encoding that is causing the problem.

    You may use the code sample from Rapid API website. However, while using RestClient, you should not urlencode the urlpath. The Rapid API engineer may make a mistake here. In most case, a , character does not need to be urlencoded. So, you can directly use https://microsoft-azure-microsoft-computer-vision-v1.p.rapidapi.com/analyze?visualfeatures=Categories,Tags,Color,Faces,Description as the path string.

    And, anyway, the correct encoded string is https://microsoft-azure-microsoft-computer-vision-v1.p.rapidapi.com/analyze?visualfeatures=Categories%2cTags%2cColor%2cFaces%2cDescription

    The code sample from Rapid API: enter image description here

    I used the sample, and got the same error message as yours. But I solved it by using the original string or the correct encoded string:

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

    And you can also use RestClient's method to add query string:

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

    And the successful result:

    enter image description here