Angular 2, 4 HTTP (get, post, put, delete, etc...) assign these methods dynamically

1.7k Views Asked by At

I want to add http methods dyamicly based on my conditions.

var obj = {
  fieldType:"button"
  label:"Submit"
  method:"put" //get, post, delete, etc...
  name:"submit"
  submitUrl:"http://apiurl.com"
  type:"submit"
}

How to assign coming object method in http.

if( obj.method ){
 this.http.obj.method('www.google.com')
 ...
}
1

There are 1 best solutions below

3
zgue On

EDIT:

I forgot you want it for angular4. The request signature is different then:

if( obj.method ){
  this.http.request(obj.method, "www.google.com")
  ... 
  }
}

In angular5 the the method can be set in the RequestOptions

const options = new RequestOptions({

  method: RequestMethod.Post //GET,PUT...

});

And the HttpClient has a request method for this purpose.

this.http.request("www.google.com", options)
     ... 
}