Reputation: 659
Using the code from the MS Cognitive services example app at https://learn.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts/csharp I get Error 404 when trying to run the following code with imageFilePath set a local JPEG file:
static async void MakeAnalysisRequest(string imageFilePath)
{
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters. A third optional parameter is "details".
string requestParameters = "visualFeatures=Description&language=en";
// Assemble the URI for the REST API Call.
string uri = uriBase + "?" + requestParameters;
HttpResponseMessage response;
// Request body. Posts a locally stored JPEG image.
byte[] byteData = GetImageAsByteArray(imageFilePath);
using (ByteArrayContent content = new ByteArrayContent(byteData))
{
// This example uses content type "application/octet-stream".
// The other content types you can use are "application/json" and "multipart/form-data".
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// Execute the REST API call.
response = await client.PostAsync(uri, content);
// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
// Display the JSON response.
Console.WriteLine("\nResponse:\n");
Console.WriteLine(JsonPrettyPrint(contentString));
}
subscriptionKey is KEY1 from the two keys provided with my Azure subscription (in Eastern Australia location) and uribase is
const string uriBase = "https://australiaeast.api.cognitive.microsoft.com/vision/v1.0";
The combination of KEY1 and uriBase works OK with the demo of the API at
https://australiaeast.dev.cognitive.microsoft.com/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa/console
when the target file is a URL rather than a local file. The image is correctly analyzed.
Why am I getting the 404 error when trying to post from C#? Is it the fact that I am using content type application/octet-stream?
Code for GetImageAsByteArray is:
static byte[] GetImageAsByteArray(string imageFilePath)
{
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
Upvotes: 1
Views: 630
Reputation: 2973
You're missing the last portion of the request URI. It should be:
string uri = uriBase + "/analyze?" + requestParameters;
Upvotes: 4