0

I am using react native camera to take picture and make post request to send it to server to be storted there and perform some actions: here is the code in frontend for send the fetch request:

 const takePicture = async () => {
        if (camera) {
            const data = await camera.current.takePictureAsync({ quality: 0.25, base64: true });
            setTakingPic(true)

                    fetch('http://*************/users/upload', {
                        method: 'POST',
                        headers: {
                            Accept: 'application/json',
                            'Content-Type': 'application/json',
                        },
                       
                        body: JSON.stringify({
                            image: data.base64,
                            id: Id
                        }),
                    }).then((response) => console.log(response))
                        .then((res) => {

                            if (res.success === true) {
                                alert(res.message);
                                navigation.navigate('studentD')
                            }
                            else {

                                alert(res.message);

                            }
                        })
                        .catch(err => { console.log(err); });
        }
    };

and in backend:

 router.post('/upload', function (req, res) {
      var image = req.body.image
      var id = req.body.id
      var status = "on hold"
      var userid = localStorage.getItem('userId');
      console.log(image)
      fs.writeFile('../images/' + id + '.png', image, 'base64', (err) => {
        if (err) { throw err }
        else {
          connection.query("INSERT INTO face_status (status,imageinfo,user_id) VALUES (?,?) ", [status, image, userid], function (err, row) {
            if (err) {
              console.log(err);
            } else {
              res.send({ 'success': true, 'message': 'ok' });
            }
          });

    }
  })

})

when camera capture the picture and send the request the server is log the request as POST /users/upload 413 2068.099 ms - 1220 which something related to the upload limit size, I tried to set the following:

router.use(bodyParser.json({ limit: '50mb' }));
router.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));

but still same error, I also console.log(response) the response in frontend and this what I got:

{"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "2313bc80-fd1d-4059-91f8-e16ffdaaa4eb", "offset": 0, "size": 1220}}, "_bodyInit": {"_data": {"__collector": [Object], "blobId": "2313bc80-fd1d-4059-91f8-e16ffdaaa4eb", "offset": 0, "size": 1220}}, "bodyUsed": false, "headers": 
{"map": {"connection": "keep-alive", "content-length": "1220", "content-type": "text/html; charset=utf-8", "date": "Mon, 08 Feb 2021 11:21:31 GMT", "etag": "W/\"4c4-N4ip9wLgHN8MkaoeSMzYtH17uPI\"", "keep-alive": "timeout=5", "x-powered-by": "Express"}}, "ok": false, "status": 413, "statusText": undefined, "type": "default", "url": "http://*************/users/upload"}
alobe
  • 59
  • 7
  • Does this answer your question? [Error: request entity too large](https://stackoverflow.com/questions/19917401/error-request-entity-too-large) – O. Jones Feb 08 '21 at 11:50
  • @O.Jones I have checked this question before posting my question but all solution there doesn't work for me – alobe Feb 08 '21 at 11:59

0 Answers0