0

Basically this http put will act like a button and switch on a system. On postman, i can just input the URL and it works fine, but when i try to implement it on Ionic App, it doesnt do anything. (Sorry if the codes is unclear)

The following is the HTTP code generated by POSTMAN

    PUT (......) HTTP/1.1
    Host: ip
    Cache-Control: no-cache
    Postman-Token: 0bad8e4d-e717-015a-e321-d2ee05acc563
    Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

Any Idea how do i implement in an Ionic Angular 4 Application?

 data: any;

    toggle(): Observable<Response> {
        let headers = new Headers({ 'Content-Type': 'multipart/form-data' });  
        let options = new RequestOptions({headers: headers});

          return this.http.put('http:/ip/......', JSON.stringify(this.data), options).
map((res:Response) => res.json()).catch((error:any) => Observable.throw(error.json().error || 'Server error'));


      }

There is actually no error whatsoever, but just that nothing happens.

1 Answers1

0

If you want to make http call you should subscribe Observable , or it won't be called

This is the one of the main diff b/w Promise and Observable

toggle(): Observable<Response> {
        let headers = new Headers({ 'Content-Type': 'multipart/form-data' });  
        let options = new RequestOptions({headers: headers});

        return this.http.put('http:/ip/......', JSON.stringify(this.data), options).
                map((res:Response) => res.json())
                .subscribe(data => { console.log(data); }) // you are missing this line over here
                .catch((error:any) => Observable.throw(error.json().error || 'Server error'));

}

Suggestion Note :

Try to put this code in some service and not in any component

return this.http.put('http:/ip/......', JSON.stringify(this.data), options).
map((res:Response) => res.json()).catch((error:any) => Observable.throw(error.json().error || 'Server error'));
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122