Angular 5 HTTP Get request Parameters - Send multiple objects

John

I am trying to make an HTTP get request with an object as param. It's this possible? I already tried it multiple ways but without success. It works if I send only strings, not the entire class

export class City {
  id: number;
  name: string;
}

  const params = new HttpParams().set('city', city)
  return this.http.get('http://localhost:3000/api/place/', {params})
funkizer

Here's a little helper function that JSON-encodes any Object-like properties in an object, leaves primitives as string. Obviously you need to decode the query in the backend. Change _.isObjectLike to some other way of checking the type if you don't want lodash.

require * as '_' from 'lodash';
private objectToHttpParams(obj: any) {
    return Object.entries(obj || {})
      .reduce((params, [key, value]) => {
        return params.set(key, _.isObjectLike(value) ? JSON.stringify(value) : String(value));
      }, new HttpParams());
  }

// use:
const params = this.objectToHttpParams({city});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to programmatically send a HTTP request with parameters?

Send HTTP GET request with header

Get HTTP GET parameters from Restlet request

Angular2 - Http POST request parameters

Angular 4 http get with multiple params to Spring Request Mapping

Send an http get request with parameters in Node.js

How to send Multiple Http request sequentially in Angular

Angular 2 http - send request conditionally

angular http post send complex objects

python send http request with multiple parameters with same name

How to send multiple SQL query parameters in Web API GET request in AngularJS?

How to send firebase jwt getIdToken to http get request in angular 8?

How test request http with parameters with jest - angular

Angular multiple input search request with http request

Send JSON body with HTTP get request

Angular: Send more than two arguments in HTTP get request

Ushahidi GET request with multiple parameters

HTTP GET request with parameters

Send JSON by ajax and get parameters by request in JSP

angular send cookies from http request to php

Send content body with HTTP GET Request in Java

Single Angular Controller w/ multiple $HTTP Get request

How to send query parameters in a sequence (order) using http get method (Angular js)

Resolve multiple promises and send angular 2 http request

How to send an HTTP GET request using Firebase and Angular?

Angular 2 - Multiple HTTP get request and cache response

How to send http request with multiple data arguments

How to send body in a HTTP Get Request Angular 9

prevent multiple http request send by using fetch