How can I pass multiple parameters in an HTTP request at Angular 6?

Asked

Viewed 533 times

-1

Below I have my class of services, but when performing the search with parameters, the parameters are not passed to the request. Can someone help me?

import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

import * as moment from 'moment';

export interface LancamentoFiltro{

  descricao: string;
  dataVencimentoInicio: Date;
  dataVencimentoFim: Date;

}

@Injectable({
  providedIn: 'root'
})
export class LancamentoService {

  lancamentosUrl = 'http://localhost:8080/lancamentos';

  constructor(private http: HttpClient) { }

  pesquisar(filter: LancamentoFiltro) : Observable<any>{

    let params = new HttpParams();
    let headers = new  HttpHeaders().set('Authorization', 'Basic dmFsZGlvbm9yanVuaW9yQG91dGxvb2suY29tOkJ3aTI4MDI4MSo=');

    if(filter.descricao){ 
      params.set('descricao', filter.descricao);
    } 

    if(filter.dataVencimentoInicio){
      params.set('dataVencimentoDe', moment(filter.dataVencimentoInicio).format('YYYY-MM-DD'));//uso a biblioteca moment para formatar a data
    } 

    if(filter.dataVencimentoFim){
      params.set('dataVencimentoAte', moment(filter.dataVencimentoFim).format('YYYY-MM-DD'));//uso a biblioteca moment para formatar a data
    } 

    return this.http.get<any[]>(`${this.lancamentosUrl}?resumo`,{headers, params});
  }
}
  • need to pass when there are parameters for the HTTP request, using Httpparams, but using the above form, parameters are not passed.

  • is in Portuguese

  • thanks for reviewing the text, and sorry, I do not have much custom on the site.

  • Quiet, read the [tour] to see how it works, and you can also go to [help] for help

  • Do so: params = params.set('descricao', filter.descricao); do the same with others and see if it works.

  • Edited* doing so as suggested the params still comes empty when I step more than one parameter.

  • 1

    Thank you, yes you solved your suggestion. Thank you very much!

Show 2 more comments

1 answer

1


Do it like this: params = params.set('Description', filter.Description); do the same with >others and see if it works. - Noobsaibot

Browser other questions tagged

You are not signed in. Login or sign up in order to post.