Home > Mobile >  Angular requisition http method Get
Angular requisition http method Get

Time:10-01

I have a problem with an http request via the post method. When then send to the back end that has an endpoint mapped as

My CarController

@RequestMapping(value = "/car")
@AllArgsConstructor
@CrossOrigin
public class CarController {

@PostMapping(value = "/test/{code}")
    public ResponseEntity<String> getCars(@PathVariable(required = true) String code) throws URISyntaxException {
        return ResponseEntity.ok("Response OK");
    }
}

My environment

export const environment = {
  production: false,
  apiEndpoint: "http://localhost:8080/my-app/"
};

My app.service.component.ts

httpOptions = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
  };

getCars() {
    const url = `${environment.apiEndpoint}`  "/car/teste/?"   `${code}`;
    this.http.get<any>(url).subscribe(resp => {
    console.log(resp)
   });
}

Error log

The request has a 404 error with the following message

ERROR {body: {…}, url: 'http://localhost:8080/car/test/', headers: HttpHeaders, status: 404, statusText: 'Not Found'}body: {error: "Collection 'car' not found"}headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}status: 404statusText: "Not Found"url: "http://localhost:8080/my-app/car/test"[[Prototype]]: Object

CodePudding user response:

Not sure if you mean making a get request, since the methods above are get requests.

Anyway, double check your URL spellings, for in the API endpoint, the URL is /test/{code}, yet in the client, the URL is ".../teste/?"...

The e in teste may be causing the 404

CodePudding user response:

You're passing the code param as a Query param, not like a param of the route. That's why it doesn't find the car.

  • Related