Skip to main content

'Content-Length' Header affecting parsing of request Gin Gonic

I am making an app where an image is taken by the user then sent to the backend as json with some other keys as base64 data. When I try to send the content from my frontend (sveltekit) it doesn't work and Gin gives me either

invalid character 'o' looking for beginning of value
// or
EOF

depending on whether I send the Content-Length header with the request.

When I send the same request from postman (which autosets the Content-Length header) it works. I tried implementing the content-length header in my frontend - see below.

dataurl is a b64 string from canvas.toDataURL()

const uploadImage = () => {
        // console.log(dataURL);
        let frdurl = dataURL.toString();
        frdurl = frdurl.split(",", 2);
        let tos = frdurl[1];
        console.log(typeof(tos));
        console.log(tos)

        let b = {
                data: tos,
                User: "test2"
            }
        fetch("http://localhost:8080/uploaddaily", {
            method: 'POST',
            mode: 'cors',
            cache: 'no-cache',
            cors: 'no-cors',
            redirect: 'follow',
            referrerPolicy: 'no-referrer',
            headers: {
                'Content-Length': JSON.stringify(b).length.toString(), // <-- THIS IS THE MAIN PART
            },
            body: b
        })
        .then(res => {
            return res.json()
        });
    }
Via Active questions tagged javascript - Stack Overflow https://ift.tt/dvVfGw8

Comments