I am trying to upload an image to MediumApi. But I can't handle it. Here is the medium example request.
POST /v1/images HTTP/1.1
Host: api.medium.com
Authorization: Bearer 181d415f34379af07b2c11d144dfbe35d
Content-Type: multipart/form-data; boundary=FormBoundaryXYZ
Accept: application/json
Accept-Charset: utf-8
--FormBoundaryXYZ
Content-Disposition: form-data; name="image"; filename="filename.png"
Content-Type: image/png
IMAGE_DATA
--FormBoundaryXYZ--
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json; charset=utf-8");
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_token}");
client.DefaultRequestHeaders.Add("ContentType", "multipart/form-data; boundary=FormBoundaryXYZ");
var requestContent = new MultipartFormDataContent("FormBoundaryXYZ");
var imageContent = new ByteArrayContent(File.ReadAllBytes(Startup.ImageFilePath + fileName));
//streamContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse($"form-data; name=image; filename={fileName}");
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
requestContent.Add(imageContent, "image", fileName);
var response = await client.PostAsync(_imageUrl, requestContent);
string resultCode = response.StatusCode.ToString();
string responseBodyAsText = await response.Content.ReadAsStringAsync();
and this is my code. This code returns Internal Server Error but I didn't understand.
question from:
https://stackoverflow.com/questions/65651317/image-upload-using-httpclient-multipartformdatacontent-in-net-core 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…