Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - Http Get request - pass json Object

How can I do a http get request and pass an json Object

This is my json-Object

{{firstname:"Peter", lastname:"Test"}

and this Object I want to pass in the http request to get a list Of matched persons.

how is it possible? This example just shows a simple get request with a json result. How do I have to modify it?

//Component:

person:Person;
persons:Person [];
....
//Whre can I pass the person, here in the service??
getMatchedPersons(){
  this.httpService.getMatchedPersons().subscribe(
     data =>  this.persons = data,
    error => aller(error)
    );
 ); 
  

  //SERVICE
  //pass parameters?? how to send the person object here?
  getMatchedPersons(){
    return this.http.get('url').map(res => res.json());
  }
like image 603
hamras Avatar asked Jun 23 '16 14:06

hamras


People also ask

How pass JSON object in HTTP get?

To send a JSON object or an array as a parameter in HTTP requests ( GET or POST ) in JavaScript, you first need to convert it into an string using the JSON. stringify() method. Next, use the encodeURIComponent() method to encode the JSON string.

What is the use of HttpClient get () method?

Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.

What is HTTP get in angular?

get() request method. HttpClient. get() method is an asynchronous method that performs an HTTP get request in Angular applications and returns an Observable. And that Observable emits the requested data when the response is received from the server.


1 Answers

The Http.get method takes an object that implements RequestOptionsArgs as a second parameter.

The search field of that object can be used to set a string or a URLSearchParams object.

An example:

 // Parameters obj-
 let params: URLSearchParams = new URLSearchParams();
 params.set('firstname', yourFirstNameData);
 params.set('lastname', yourLastNameData);

 //Http request-
 return this.http.get('url', {
   search: params
 }).subscribe(
   (response) => //some manipulation with response 
 );
like image 171
Roman Skydan Avatar answered Sep 19 '22 05:09

Roman Skydan