I try to mirror images on my own image hoster, which contains a simple API that accepts default form data uploads like this:
-----------------------------149841124823007
Content-Disposition: form-data; name="file"; filename="ZMVdEwM.png"
Content-Type: image/png
<Binary image data...>
This upload was tested using a simple html form and works well. Now I want to use this API in an .NET Core Standard application. Found different examples:
string url = "https://i.stack.imgur.com/uwGdv.png";
var client = new HttpClient();
var imageData = client.GetByteArrayAsync(url).Result;
var content = new MultipartFormDataContent($"-----------------------------{DateTime.Now.Ticks}");
content.Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");
string fileName = new Uri(url).LocalPath.Replace("/", "");
content.Add(new ByteArrayContent(imageData), "file", fileName);
var postResp = client.PostAsync("https://my-image-hoster/api.php", content).Result;
string resp = postResp.Content.ReadAsStringAsync().Result;
Console.WriteLine(resp);
I'm downloading the test image https://i.stack.imgur.com/uwGdv.png as byte array and try to build the same form-data upload. But it fails here on my api:
if (!isset($_FILES['file'])) {
echo json_encode(array('errorcode' => 'no_image_transfered'));
}
While investigation the problem, I inspected the MultipartFormDataContent
instance called content
since it's responsible for building the request body. It shows a ContentDisposition
property containing the file name twice: The first one is correct, but the second one looks maleformed:
What's wrong with my POST request?