MineSkin / api.mineskin.org

Source of the MineSkin API
https://api.mineskin.org
MIT License
50 stars 5 forks source link

POST request trouble with reading a file #344

Closed pjesek closed 3 years ago

pjesek commented 3 years ago

Hi, I have a problem with making a POST request to Mineskin API and it's getting trouble with reading a file object. Maybe it's not that related issue but it could help everyone in the future.

Here's the error:

TypeError: source.on is not a function
    at Function.DelayedStream.create (C:\Users\PC\custom-skin-uploader\skin-uploader-api\node_modules\delayed-stream\lib\delayed_stream.js:33:10)  
    at FormData.CombinedStream.append (C:\Users\PC\custom-skin-uploader\skin-uploader-api\node_modules\combined-stream\lib\combined_stream.js:45:37)
    at FormData.append (C:\Users\PC\custom-skin-uploader\skin-uploader-api\node_modules\form-data\lib\form_data.js:74:3)
    at C:\Users\PC\custom-skin-uploader\skin-uploader-api\server.js:21:14
    at Layer.handle [as handle_request] (C:\Users\PC\custom-skin-uploader\skin-uploader-api\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\PC\custom-skin-uploader\skin-uploader-api\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\PC\custom-skin-uploader\skin-uploader-api\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\PC\custom-skin-uploader\skin-uploader-api\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\PC\custom-skin-uploader\skin-uploader-api\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\PC\custom-skin-uploader\skin-uploader-api\node_modules\express\lib\router\index.js:335:12)

Here's what I've actually made. Any solutions? i tried everything.


app.post('/upload', (req, res) => {
    if(req.files === null) {
        return res.status(400).json({ msg: 'No file uploaded'});
    }
    console.log(req.files.file);

    const formData = new FormData();
    formData.append('file', req.files.file);
    formData.append('api', API_KEY);

    axios.post('https://api.mineskin.org/generate/upload', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    }).then((res) => {
            console.log(`Status: ${res.status}`);
            console.log('Body: ', res.data);
        }).catch((err) => {
            console.error(err);
        })
});
InventivetalentDev commented 3 years ago

you need to include the FormData boundary headers:

const formData = new FormData();
    formData.append('file', req.files.file);
    formData.append('api', API_KEY);

    axios.post('https://api.mineskin.org/generate/upload', formData, {
        headers: formData.getHeaders()
    }).then((res) => {
            console.log(`Status: ${res.status}`);
            console.log('Body: ', res.data);
        }).catch((err) => {
            console.error(err);
        })
pjesek commented 3 years ago

you need to include the FormData boundary headers:

const formData = new FormData();
    formData.append('file', req.files.file);
    formData.append('api', API_KEY);

    axios.post('https://api.mineskin.org/generate/upload', formData, {
        headers: formData.getHeaders()
    }).then((res) => {
            console.log(`Status: ${res.status}`);
            console.log('Body: ', res.data);
        }).catch((err) => {
            console.error(err);
        })

still same error unluckily

InventivetalentDev commented 3 years ago

try removing the formData from the argument list and add it as body: formData below headers

pjesek commented 3 years ago

try removing the formData from the argument list and add it as body: formData below headers

Same error appears. There's something with file, maybe cause I've already requested it through react client to express server Here's the frontend (react) side:


  fileUploadHandler = () => {
    const formData = new FormData();
    formData.append('file', this.state.selectedFile);

    try {
      axios.post('/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        },
      });

      console.log('File Uploaded');
    } catch (err) {
      if (err.response.status === 500) {
        console.log('There was a problem with the server');
      } else {
        console.log(err.response.data.msg);
      }
    }
  }
pjesek commented 3 years ago

I also console logged an uploaded file object on express side, where this error appears. (Maybe that will help with finding the problem)


{
  name: 'e00f06e2b715fec1.png',
  data: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 40 00 00 00 40 08 03 00 00 00 9d b7 81 ec 00 00 02 1f 50 4c 54 45 00 00 00 00 00 00 00 a4 f6 ... 1486 more bytes>,
  size: 1536,
  encoding: '7bit',
  tempFilePath: '',
  truncated: false,
  mimetype: 'image/png',
  md5: '7df6d883759491df028ed2c0a4edb7c8',
  mv: [Function: mv]
}
pjesek commented 3 years ago

Okay, so there's an error with the /generate/upload endpoint. I used rest.wiki upload, even when i chose a skin file after executing it the error appears:


{
  "error": "missing files"
}
stale[bot] commented 3 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.