POST multipart/form-data in Ionic Native HTTP


#1

Hi All

Is there a way to do POST request with multipart/form-data in ionic native HTTP ?


#2

@Kanishk12

try below example

https://devdactic.com/ionic-2-images/

thanks


#3

@vd_virani, Thanks for replying.

It is a great example, but to use this i’ll have to change a whole lot of my code. Below are some lines from my code, just see if you can suggest something in this approach:

let clientConfirmData = new FormData();
var blob = new Blob([this.signature], { type: ‘image/png’ });

clientConfirmData.append(“patientConfirmation”, blob);

this.http.post(url, clientConfirmData, headers)
.then(data =>{

                                        })
                                        .catch(error => {
                                          
                                        }));

so, my question is does clientConfirmData has to be a JSON object for it to work, won’t it work with FormData type object ? ( http is from ionic native’s not angular’s)

Thanks


#4

Did you ever solve this? I need to send as multipart/form-data as required by the server


#5

No, Ionic’s Native Http does not support FormData type objects, But you can use Angular’s Http to achieve the same.
In my case, the backend guys had to change the service to accept JSON Object instead of FormData Object.


#6

Ionic does support FormData. To those who are looking for answer, following codes work

postFile(token:string, file:Blob){

        let url = WebService.API_POST_FILE + "?token="+token;
        let httpOptions = {
            headers: new HttpHeaders({
                'enctype': 'multipart/form-data; boundary=----WebKitFormBoundaryuL67FWkv1CA'
            })
        };
        
        let formData = new FormData();
        formData.append('file', file, 'test.jpg');

        console.log("post photo to URL at "+url);
        return this.http
            .post<SimpleResponse>(
                url,
                formData,
                httpOptions
        );
    }

#7

Hey, this is angular’s http service. The question is for Ionic Native HTTP