Hello again
Still working away on my little client. I have managed to get text posts to get created using the Posting API, but I’m a little stumped on getting an image uploaded. Any help or guidance would be appreciated. I making a POST request to my server, reading the file data, getting the media-endpoint and then attempting to POST the image. Below is the the code I’m working with:
const body = ctx.request.body({ type: 'form-data' });
const data = await body.value.read({ maxSize: 10000000 });
const file = data.files != undefined? data.files[0] : undefined;
if(file) {
let fetchMethod = {
method: "GET",
headers: {
"Authorization": "Bearer " + access_token
}
};
let fetchingMediaEndpoint = await fetch('https://micro.blog/micropub?q=config', fetchMethod);
let endpointFetched = await fetchingMediaEndpoint.json();
let mediaEndpoint = endpointFetched["media-endpoint"];
let formData = new FormData();
formData.append('file', file);
let uploadMethod = {
method: "POST",
headers: {
"Authorization": "Bearer " + access_token
},
body: formData
};
let fetchingFileUpload = await fetch(mediaEndpoint, uploadMethod);
let fileFetched = await fetchingFileUpload.json();
}
and this is what the contents of the file object look like:
{
content: Uint8Array(37448) [
137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13,
73, 72, 68, 82, 0, 0, 2, 176, 0, 0, 2, 170,
8, 3, 0, 0, 0, 245, 215, 157, 121, 0, 0, 0,
1, 115, 82, 71, 66, 0, 174, 206, 28, 233, 0, 0,
0, 4, 103, 65, 77, 65, 0, 0, 177, 143, 11, 252,
97, 5, 0, 0, 1, 59, 80, 76, 84, 69, 255, 255,
255, 253, 253, 253, 254, 254, 254, 252, 252, 252, 249, 249,
249, 240, 240, 240, 234, 234, 234, 229, 229, 229, 222, 222,
222, 226, 226, 226,
... 37348 more items
],
contentType: "image/png",
name: "media",
filename: undefined,
originalName: "Untitled.png"
}
I don’t even get a JSON response back, but an html error page. Is the file not in the right format? The help documentation says “The media endpoint accepts a multipart/form-data
upload with a file
part containing the JPEG image data.”
Thanks!
Loura