5

I'm trying to upload Image to wordpress using Wordpress rest API.

Everything is working but, proper image is not getting uploaded. However, when i use Curl image is proper.

curl --request POST \
--url http://localhost/wordpress/wp-json/wp/v2/media/ \
--header "cache-control: no-cache" \
--header "content-disposition: attachment; filename=hh.jpg" \
--header "authorization: Basic token" \
--header "content-type: image/jpg" \
--data-binary "@C://Users/as/Desktop/App _Ideas/hh.jpg" \

This curl request is working properly.

Now, when i converted request to Nodejs

request({
        url: ' http://localhost/wordpress/wp-json/wp/v2/media/',
        headers: {
          'cache-control': 'no-cache',
          'content-disposition': 'attachment; filename='+filename,
          'content-type' : 'image/jpg',
          'authorization' : 'Basic token'
        },
        encoding: null,
        method: 'POST',
        body: "@C://Users/as/Desktop/App _Ideas/hh.jpg",
        encoding: null
       }, (error, response, body) => {
            if (error) {
               res.json({name : error});
            } else {
              res.json(JSON.parse(response.body.toString()));
            }
       });

I'm able to receive json response from wordpress as imge uploaded. But, uploaded image is not proper as binary data passed was not proper.

Wordpress uploades folder shows file hh.jpg for size 17 bytes.. On editing with notepad it show @hh.jpg which means string data is saved as hh.jpg not the actual binary data.

Any ideas how to pass binary data properly with Nodejs.

4
  • Yeah.... The body you pass is a string. I guess @path is curl's syntactic sugar. Feb 26, 2018 at 17:55
  • @alex-rokabilis yes you are right. On editing the uploaded image with notepad it show hh.jpg means the string is saved as image due to content-disposition header. Feb 26, 2018 at 18:00
  • Any ideas how to upload it correctly ? Feb 26, 2018 at 18:00
  • 1
    Yeah, try body: require('fs').createReadStream("C://Users/as/Desktop/App _Ideas/hh.jpg") ( I am not sure about the correct format of paths in windows, maybe you need backslashes?) Feb 26, 2018 at 19:59

1 Answer 1

16

Got it working , We need to create a stream of file using

fs.createReadStream(filename);

fs module.

request({
        url: 'http://localhost/wordpress/wp-json/wp/v2/media/',
        method: 'POST',
        headers: {
          'cache-control': 'no-cache',
          'content-disposition': 'attachment; filename=' + filename,
          'content-type' : 'image/jpg',
          'authorization' : 'Basic token'
        },
        encoding: null,
        body: fs.createReadStream('C://Users/as/Desktop/App _Ideas/hh.jpg')
       }, (error, response, body) => {
            if (error) {
               res.json({name : error});
            } else {
              res.json(JSON.parse(response.body.toString()));
            }
       });
3
  • Hi. this does not work for me. i should send a tar file (binary data) with a POST http request. I can do it with Postman but now i cannot do it. i code it as you code, with a little deference : headers: { 'Content-Type': 'application/x-tar' } and body: fs.createReadStream(tarFilePath, { encoding: 'binary' }) but it does not send file as postman send. can you give me a little help? or update your answer with axios. because request deprecated. Sep 24, 2020 at 6:33
  • can you tell me how to download those files ?
    – Monu
    Oct 5, 2020 at 13:52
  • 1
    Thank you so much bro. I almost destroyed my laptop because I couldn't get this working
    – Nick.K
    Jun 4, 2021 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.