Have a problem with REST API for creating media
Trying post image to WordPress and got the good result – media created and Postman got response with id of the new image.
But when I run request from my application I got next result:
- media created
- very long time waiting for the response and then got Gateway timeout
create_media(filePath, fileName, fileOption, wp);
function retrieveImageBuffer(url){
return axios
.get(url, {
responseType: "arraybuffer"
})
.then(response => Buffer.from(response.data, "binary"))
.catch(function(err){
console.error(err);
});
}
async function create_media(url, fileName, fileOption, wp){
let buffer = await retrieveImageBuffer(url);
try {
let result = await wp
.media()
.file(buffer,fileName)
.create(fileOption)
.catch(function(err){
console.error(err.body);
});
console.log(result);
} catch (err) {
console.error(err.message);
}
}
I need id from response for added futured image to the post
When I use REST API from my application to creating a post all work well.
UPDATE 2
And try WP REST API
var url = base_link + '/wp-json/wp/v2/media';
var headers = {
"Authorization" : base_auth,
"Cache-Control": "no-cache",
"Content-Disposition": 'attachment; filename="'+fileName+'"'
}
var requ = request.post({url:url,headers:headers,method:'POST'},
function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log(body);
})
var form = requ.form();
form.append('file', fs.createReadStream('file_uploader/'+fileName));
form.append('title', img_title);
form.append('alt_text', alt_text);
form.append('post', post_id);
same result