最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WP REST API Create media return Timeout Gateway in response, but media created

programmeradmin0浏览0评论

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:

  1. media created
  2. 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

发布评论

评论列表(0)

  1. 暂无评论