0

I'm implementing a login system using based on two requests. The first request yields the userId, and the userId is passed to the second request to find the userRole, which provides authentication status and redirection to relevant dashboard.

From the API side, both requests take header options and body parameters. Additionally the second request takes the token provided by the first request. I'm able to pass header options to the first call and it's successful:

this.http.post<any>(`${environment.baseUrlUserSecurity}/users/login`, data, Option)

but the second call:

this.http.get(`${environment.baseUrlUserSecurity}/prsnls`, userId, RoleOptions)

doesn't accept the header option RoleOptions. This is the code:

login(data){
      const Option ={ headers: new HttpHeaders({'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json',"USER_CODE":data.USER_CODE})};
      
      //see that you "return" an observable
      return this.http.post<any>(`${environment.baseUrlUserSecurity}/users/login`, data, Option)
      .pipe(
       switchMap(value=>{
          const userId = value[0].result.userId
          const currentToken = value[0].result.token
          const RoleOptions = { headers: new HttpHeaders({
                            'Access-Control-Allow-Origin': '*', 
                            'Content-Type': 'application/json', 
                            'Authorization : 'Bearer' + currentToken, 
                            'USER_CODE':data.USER_CODE
                            )};
                          }
          return this.http.get(`${environment.baseUrlUserSecurity}/prsnls`, userId, 
                           RoleOptions)

       }).pipe(map(user =>(
         { ...user}
       )))

But VS Code complains about the RoleOptions which is the third argument passed to the get request within the switchMap saying that only "1 or 2 arguments are needed, but received 3". Why can't I pass header options to the GET request added to the body parameter passed to it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Himmels DJ
  • 395
  • 5
  • 20
  • Did you read the API docs? They tell you what the arguments to each method are - GET requests, unlike POSTs, don't (semantically, at least) have a body. – jonrsharpe Apr 25 '22 at 15:42
  • @jonrsharpe Thanks, but how do I then deal with such situation? I'm getting the data from backend and in postman, the get request required a token in authorization, a userId and the USER_CODE in the header option. How do I implement that? – Himmels DJ Apr 25 '22 at 15:51
  • We don't know. You haven't told us enough about the request you're trying to make. You seem to be trying to make the GET with the user ID as the _body_ of the request, which [doesn't really make sense](https://stackoverflow.com/q/978061/3001761) (although sadly that doesn't rule out it being what the API expects...). But again the API docs tell you how to use the methods - `get` expects a URL and, optionally, the options object (including headers, etc.). _"Why can't I pass header options to the GET request?"_ - you can, but it's not the third parameter. – jonrsharpe Apr 25 '22 at 15:53
  • @jonrsharpe this is the context described in a previous question about mapping multiple request https://stackoverflow.com/questions/71958770/i-can-not-subscribe-on-mergemap-observable-in-angular-13 – Himmels DJ Apr 25 '22 at 16:00
  • 1
    Please [edit] so that the relevant context and a minimal example (only the GET is relevant, no?) is in _this question_. The other one doesn't seem to show exactly what request you're trying to make either, though. – jonrsharpe Apr 25 '22 at 16:00
  • Again you _can_ pass the header options. You can't pass the _body_, because an XHR won't send a GET with a body (see e.g. https://stackoverflow.com/a/70118101/3001761). The problem is with the API design, if that's actually what's needed. – jonrsharpe Apr 25 '22 at 16:28
  • Ok @jonrsharpe, everything is very clear to me now. I'm going to review the api architecture. Thanks – Himmels DJ Apr 25 '22 at 17:05

0 Answers0