Badrequest 400 Httpparams Angular 4.3

Asked

Viewed 233 times

0

I’m using the new library of angular to make requisitions, the HttpClient. Along with the 4+ angle I am using here in the company for the backend, Java with Spring (boot).

The problem is occurring at the time of making a GET request to the backend, the HttpParams, used with HttpClient to pass parameters, does not do the replace of the variables and thus the backend denies the request.

Ex.: URL backend (Java): controller/obterByCodOne/{codOne}/codTwo/{codTwo}

Ex.: URL frontend (Angular): http://localhost:8080/controller/obterByCodOne/:codOne/codTwo/:codTwo

Code:

let params = new HttpParams()
            .set('codOne', codOne)
            .set('codTwo', codTwo);

Result (wrong): http://localhost:8080/controller/obterByCodOne/:codOne/codTwo/:codTwo?codOne=valueA&codTwo=valueB

The HttpParams is not replacing the variable, but concatenating.

In other projects Angularjs was replaced the value normally using the $resource. Instead of having q concatenate pieces of URL’s with variables until a final result is reached.

Anyway... I’m in this jam. Either I don’t understand the use of Httpparams or I don’t know how to use it.

  • Tried to leave the url only controller/obterByCodOne?

  • @Lucascosta already yes. Thus returns 404 :\

  • @LINQ which file more you need? I just put this because I thought that was enough.

  • Every link I encounter shows you mounting the url in your hand. Ex: http://javasampleapproach.com/frontend/angular/use-angular-httpclient-post-put-delete-data-springboot-rest-apis-angular-4

3 answers

1

The HttpParams are the QueryParams, the url parameters ?.

Use Typescript String Interpolation to mount your url.

get(codOne: number, codTwo: number): Observable<any> {
  return this.httpClient.get(`http://localhost:8080/controller/obterByCodOne/${codOne}/codTwo/${codTwo}`);
}
  • I found another solution. But thank you!

1

This problem has already occurred with me. If I can’t use Httpparams, make a String manipulation and manually add the params. ex.:

  public getPessoas(idade){

       this.http.get(URL_API+"?idade="+idade)
       ...
  }

Obs.:

  • URL_API - Is the url of the api
  • "?age" - This is the manual way to add a parameter.
  • age - variable representing the value I am passing as parameter.

If you want to add a new parameter, add a "&" and pass the new parameter. ex.:

this.http.get(URL_API+"?idade="+idade+"&peso="+peso)

This is not the best solution, but it works! I hope it helped :)

  • If I use Httpparams, he’ll do it for me. The problem is that I define the variables in my back in the url itself, as in the example I gave up. But I’ll probably opt for that option anyway. I’m analyzing the best case and then comment here. Vlw guy!

0


As my URLS were in a standard URLS file, I could not concatenate. I ended up adopting a pattern for my URLS and each parameter is followed by two points, example: http://minhaurl.com/:ida/:nomeB and in my service I give a replace. In the Java backend I use @Pathvariable in my parameters, with @Requestmapping I used my url like this http://minhaurl.com/{ida}/{nomeB}.

In this scenario, the parameters should be mandatory. In a filter case with opitional parameters I adopt the practice of using DTO (Data Transfer Object).

I hope to help you!

Browser other questions tagged

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