Reputation: 623
I am trying to do image upload with base64 format using httpclient, anybody has experienced on this?
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var detect = new Dictionary<string, string>
{
{ "api_key", API_KEY },
{ "api_secret", API_SECRET },
{ "urls", _base64 }
};
var content = new FormUrlEncodedContent(detect);
var response = await client.PostAsync(DETECT_URL, content);
HttpStatusCode _statusCode = response.StatusCode;
}
the above codes returns BadRequest, please help
many thanks in advance
Don
Upvotes: 0
Views: 744
Reputation: 207
a,
FormUrlEncodedContent
is application/x-www-form-urlencoded
not application/json
.
b,
If you have the image loaded you can convert it to base64 using this:
public string ImageToBase64(Image image)
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
Upvotes: 1