C
C#•3y ago
DiamondKesha

Send image to API with HttpClient Post

Hello. How to correct send image to api? Used several ways, but for some reason I get BadRequest. In API documentation they use this example nodejs code: await fetch("url", { method: "POST", body: fs.readFileSync("demo.jpg"), headers: { "Content-type": "image/jpeg" }, }).then((e) => e.json());
1 Reply
undisputed world champions
the example seems to send the bytes directly as the body but you are sending formdata i would expect something like this:
var bytes = await File.ReadAllBytesAsync(filePath);
HttpContent content = new ByteArrayContent(bytes);
await client.PostAsync(url, content);
var bytes = await File.ReadAllBytesAsync(filePath);
HttpContent content = new ByteArrayContent(bytes);
await client.PostAsync(url, content);
or similar 😉
Want results from more Discord servers?
Add your server