简体   繁体   中英

how to convert JSON into multipart/form-data

How I Can Convert JSON into Form Data

let data = {
        image : this.state.file,
        title : this.state.title,
        description: this.state.description,
      }

Try this way

let data = `{
        "image": ${this.state.file},
        "title" : ${this.state.title},
        "description": ${this.state.description},
      }`;

const jsonData = JSON.parse(data);

var fdata = new FormData();

fdata.append('image', {
    uri: jsonData.image,
    name: 'photo.jpeg',
    type: 'image/jpeg'
});
fdata.append('title',jsonData.title);
fdata.append('description',jsonData.description);

Something like this:

let form_data = new FormData();

form_data.set('data', {
    "image": this.state.file,
    "title" : this.state.title,
    "description": this.state.description,
  });

ax({
    url: '/api',
    method: 'post',
    data: form_data,
    headers: {'content-type': 'multipart/form-data'}
})
.then(response => {
})
.catch(error => {
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM