mercredi 9 décembre 2020

Appliaction in Spring Boot and ReactJS gives error

I am trying to test file upload in JMeter but when I do that I'm getting message: Cannot POST. I'm trying to upload files and send them in format application/json and then save them to database. Application does what it supposes to do but when it comes to testing I'm not getting the expected results. I have application written in ReactJS and Spring Boot. Here are some code snippets: ReactJS:

    constructor(props) {
        super(props);
        this.state = {
            files: [],
        };
    }

    submitForm(contentType, data, setResponse) {
        axios({
            url: `${URL}`,
            method: 'POST',
        }).then((response) => {
            setResponse(response.data);
        })
    }


    async onHandleChange(e)  {

        let files = e.target.files;
        let allSelectedFiles = [];

        for (let i = 0; i < files.length; i++) {
            let file = e.target.files[i];


            const toBase64 = file => new Promise((resolve, reject) => {
                let reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onload = () => resolve(reader.result);
                reader.onerror = error => reject(error);
            });
            let fileLabel = {
                name: file.name,
                type: file.type,
                size: Math.round(file.size / 1000),
                base64: await toBase64(file),
            };

            allSelectedFiles.push(fileLabel);

            let jsonObject = JSON.stringify(allSelectedFiles);

    }

        this.submitForm("application/json", allSelectedFiles, (msg) => console.log(msg));

Spring Boot:

@PostMapping("/files")
    @ResponseStatus(HttpStatus.CREATED)
    public Flux<File> addFile(@RequestBody List<File> file){
        Flux flux = fileRepository.saveAll(file);
        return flux;
    }

Aucun commentaire:

Enregistrer un commentaire