Hi,
I've tried send image through multipart/form POST method to server.
However, send file is working. With POSTMAN, with C# code, it worked.
But I want send Bitmap(Image), not file.
with this code, it worked.
HttpClient client = new HttpClient())
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "some url");
request.Headers.Add("apiKey", "some key");
var content = new MultipartFormDataContent();
content.Add(new StringContent("TEST IMAGE"), "ImageName");
content.Add(new StreamContent(File.OpenRead("C:/Test/Test.jpg")), "Image", "Test.jpg");
content.Add(new StringContent("IMAGE CODE"), "Code");
request.Content = content;
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
What I have tried:
This code always get 400 bad request error.
What I've done wrong? How can I achieve send image, not file?
public static Stream ImageToStream(Image image, ImageFormat format)
{
var stream = new System.IO.MemoryStream();
image.Save(stream, format);
stream.Position = 0;
return stream;
}
HttpClient client = new HttpClient())
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "some url");
request.Headers.Add("apiKey", "some key");
var content = new MultipartFormDataContent();
content.Add(new StringContent("TEST IMAGE"), "ImageName");
var imageContent = new StreamContent(ImageToStream(myImage, ImageFormat.jpeg));
content.Add(imageContent, "Image");
content.Add(new StringContent("IMAGE CODE"), "Code");
request.Content = content;
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();