8

reference Image: Postman form

i want to post Form Data like that.

what should i prepare for sending image file data?

i have Uri, type, filename, size.

then will use fetch for it.

Content-type in header is 'multipart/formdata'

thanks for helping

3
  • 1
    I am trying to do the same thing. Any luck? Commented Apr 12, 2018 at 5:39
  • sorry for late reply, my solution was just put this with other form-data altogether postData.append('webmailAttachment',{ uri:res.uri, type:res.type, name:res.fileName }); Commented Apr 13, 2018 at 6:32
  • Can anyone look at this doubt and give me some suggestion Commented Feb 20, 2019 at 7:47

3 Answers 3

13

You should have an upload function, which should look like this:

upload(url, data) {
  let options = {
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    method: 'POST'
  };

  options.body = new FormData();
  for (let key in data) {
    options.body.append(key, data[key]);
  }

  return fetch(requestUrl, options)
      .then(response => {
        return response.json()
          .then(responseJson => {
            //You put some checks here
            return responseJson;
          });
      });
}

And you call it this way, sending the image blob path:

this.upload('http://exampleurl.com/someApiCall', {
  file: {
    uri: image.path,
    type: image.mime,
    name: image.name,
  }
}).then(r => {
  //do something with `r`
});
Sign up to request clarification or add additional context in comments.

1 Comment

you should add the await at the fetch call in order to simplify the snippet. By other way, there would no reason to mark as "async" the function.
0

You need to create an instance of FormData and pass that as the body to fetch, like so:

const data = new FormData()
data.append("something", something)

fetch(url, { method: 'POST', body: form })

1 Comment

they required Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA should i put some specific code for boundary??
0

React Native code

fetch("url" ,{

    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },

    method:'POST',

    body: JSON.stringify(this.state.imageholder)

}).catch(function(error) {

console.log('There has been a problem with your fetch operation: ' + error.message);

throw error;

});

Spring boot code

 @PostMapping(value="/",consumes = MediaType.APPLICATION_JSON_VALUE)
 public ResponseEntity<?> saveCustomerOrder(@RequestBody String[] file) throws SerialException, SQLException {

    TestImg imgObj=null;

    for (String img : file) {
        imgObj=new TestImg();
        byte[] decodedByte = Base64.getDecoder().decode(img);
        Blob b = new SerialBlob(decodedByte);
        imgObj.setImg(b);
        testRepo.save(imgObj);

    }

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.