-1

How I Can Convert JSON into Form Data

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

2 Answers 2

3

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 => {
});
Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.