Search code examples
angulartypescriptes6-promise

Http post requests in angular


I'm sending two HTTP requests. The first one is the Login request for authorizing and a second request for creating some user data. The Login call works fine but on the second call I'm getting the error :

ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},
"lazyUpdate":null},
"status":404,
"statusText":"Not Found",
"url":"localhost:4200/… failure response for localhost:4200/api 404 Not Found"
    at resolvePromise (zone-evergreen.js:798) 
    at resolvePromise (zone-evergreen.js:750)
    at ZoneDelegate.invokeTask (polyfills.js:sourcemap:412)
    at Object.onInvokeTask (vendor.js:sourcemap:64550)
    at ZoneDelegate.invokeTask (polyfills.js:sourcemap:411)
    at Zone.runTask (polyfills.js:sourcemap:180)
    at drainMicroTaskQueue (polyfills.js:sourcemap:582)
    at ZoneTask.invokeTask [as invoke] (polyfills.js:sourcemap:497)
    at invokeTask (polyfills.js:sourcemap:1634)

How do I find out what or where the problem is? I assumed the error was because the second API did not have auth info from the first to work properly but passing the response of api1 to api2 does not do anything either.

The code for sending the requests where I tried passing there response. (Although I still think this isn't correct but I'm not sure and nothing else is working): service.ts

Login() {
    let promiseResult : any;
    this.http.post<any>('/Login', this.auth_payload).toPromise().then(data => {
      promiseResult = data;
      console.log("Login call" ,promiseResult)
      return promiseResult

    });
  }

    data = this.Login()
    Create(data) {
      let promiseResult : any;
      this.http.post<any>('/Create', this.create_payload).toPromise().then(data => {
        promiseResult = data;
        console.log("set Information call" ,promiseResult)
        return promiseResult

      });
    }

component.ts


  onSubmit(){
    this.authService.Login()
    let call = this.Service.Login()
    return call 

  }
  data = this.onSubmit

  SetVital(){
    let data = this.onSubmit
    this.authService.Create(data)


Solution

  • if you just add '/Login' to your api path it will requst to localhost:4200/Login for resources, thats what happening here, please try to add your correct path in requsts correct api path assume that your api is running on localhost:3000/api then change your requst like this

     Create(data) {
         const baseurl = 'localhost:3000/api/Create'
         return this.http.post(baseurl, this.create_payload);
         }