0

I have a lot of images on my website and want to migrate the images to a new hosting. then I made a script to scrape it using xray. once I got the url I want to download the file without writing it to hardisk and then directly upload it to my new hosting. I am using Strapi and "strapi-provider-upload-wasabi" to upload my image. with my code I always getting 400 Bad request

request.get(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        data = "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');

        var form = {
            "refId": body.id,
            "ref": "comic",
            "field": "cover",
            "files": data,
            "path": "/"
        }

        request({
            uri: 'http://localhost:1337/upload',
            method: 'POST',
            headers: {
                'Content-Type': 'multipart/form-data'
            },
            formData: form
        }, function optionalCallback(err, httpResponse, body) {

            if (err) {
                return console.error('upload failed:', err);
            }
            // console.log(httpResponse);
            console.log('Upload successful!  Server responded with:', httpResponse.statusMessage);
        });
    }
});
Gadonski
  • 3,150
  • 2
  • 25
  • 31
Ujang Karnadi
  • 361
  • 1
  • 4
  • 11

1 Answers1

0

First step would be to check error message.

Make sure that model comic has upload field.

response.headers["content-type"]

should be:

response.headers["Content-Type"]

Form Data should be FormData object:

https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData

Which version of Strapi do you have?

Have you checked latest documentation?

https://strapi.io/documentation/3.0.0-beta.x/plugins/upload.html

Also, it would be good to try with some up-to-date provider, e.g.

https://www.npmjs.com/package/strapi-provider-upload-local

hp10
  • 602
  • 6
  • 11
  • i can upload the image using fs.createReadStream. what i want is I have an image url, I download it as a buffer, and then upload the buffer to strapi. the documentation says it accept buffer or stream. but using buffer I still get bad request. – Ujang Karnadi Nov 21 '19 at 19:57
  • Then probably base64 format is wrong, my first guess would be that content-type is misspelled. Did you try to log `response.headers["content-type"]`? – hp10 Nov 22 '19 at 08:30