0

I have the following http POST trying to be returned to another function:

return this.http.post(ENDPOINT_URL + 'MY-JWT-VALIDATION' + token, {observe: 'response'}, {headers: headers})
 .pipe(map( (res) => { return res; } ));

However, what I'm getting from the return is an Observable object with all that data vs. the actual response of the call. I've tried changing from map to tap to subscribe and everything.

Google seems to infer I'm doing this properly, but for some reason I can't get a response back.. even if I just return a static string.

Been stuck at this for a hour. My other http.post calls seem to be working fine and carry this same logic though I pass them through a function first.

Am I overlooking something?

  • so, the return type of that snippet is `Observable`, right? and what do you expect? `Observable>`? In not sure of what you mean with _an Observable object with all that data vs. the actual response of the call_ – Jota.Toledo Feb 03 '19 at 18:54
  • Well, if you are observing the response `{observe: 'response'}` of course you will receive all the data from the response ;) Remove that to get just the data. – AT82 Feb 03 '19 at 18:54
  • @AJT_82 cant be the case, as the 3rd argument are the options of the request. The default observe is applied. – Jota.Toledo Feb 03 '19 at 19:02
  • @Jota.Toledo - The return type (from help toolbars) inside my code says `Observable`, but it is an object of `Observable` when I print it out. I am expecting to receive the actual JSON response from the server as with all my other `http.post` requests. That JSON should resemble what I get from my REST API testing program such as: `{ "code": "jwt_auth_no_auth_header", "message": "Authorization header not found.", "data": { "status": 403 } }` (so yes, I believe `Observable>` is correct and what I need from quickly checking the docs) –  Feb 03 '19 at 19:07
  • @Jota.Toledo You are correct, I looked too hasty and just focused on the `{observe: 'response'}` :D – AT82 Feb 03 '19 at 19:11
  • Is `{observe: 'response'}` really meant to be your body for the request? – AT82 Feb 03 '19 at 19:18
  • @AJT_82 - Nope! I just put that in from a tutorial I saw as I assumed it would tell this post request to throw me back the response rather than the Observable object I am getting. I can remove it and get the same thing: `this.http.post(ENDPOINT_URL + 'jwt-auth/v1/token/validate?token=' + token, {}, {headers: headers}) .pipe(map(res => JSON.stringify(res) ));`. I am expecting to get the JSON response back from my `.pipe(map..` scenario, and should be able to put `res => { return 'test' }` in there and get back the 'test', no? I'm not getting back what I think I should be –  Feb 03 '19 at 19:20
  • Could you just for clarity's sake also add the component code for this as well. Seems a bit odd to me this :) – AT82 Feb 03 '19 at 19:29
  • @AJT_82 - Added to the bottom of the original for you; it is just a single Service for authentication. The function we are looking at is `validateAuthToken` and it is being passed a token from where it is called. JSON is literally just going to return either a success if the token is valid or a failure if not, but I can't get that JSON response returned. –  Feb 03 '19 at 19:34
  • well that explains it, you are not subscribing to the observable you get, but setting it directly to storage. You need to subscribe. – AT82 Feb 03 '19 at 19:35
  • @AJT_82 - I think you are looking at the wrong function? Everything else here works but the `validateAuthToken` function which only returns. –  Feb 03 '19 at 19:37
  • I'm maybe getting tired here :D But where is the code which calls this `validateAuthToken`? That was what I meant to ask for when I asked for the component code :) The part where you console log and get that output you are showing. – AT82 Feb 03 '19 at 19:40
  • @AJT_82 - It's just a single line setting that return to a variable on a page so I can console.log() and print it out on the HTML: `this.test = this.authenticationService.validateAuthToken(localStorage.getItem('jwt_token'));` . The token gets passed through successfully. –  Feb 03 '19 at 19:42
  • So you are printing `test`? Still, that **is** an observable, so expected output in that case. – AT82 Feb 03 '19 at 19:44
  • @AJT_82 - I am just printing `test`! If that's expected, then how do I get the actual response of my http.post request? I need the http POST's response. –  Feb 03 '19 at 19:45
  • You need to **subscribe** :) – AT82 Feb 03 '19 at 19:45
  • `this.authenticationService.validateAuthToken(localStorage.getItem('jwt_token')).subscribe(data => this.test = data)` – AT82 Feb 03 '19 at 19:46
  • @AJT_82 - Like such? `this.authenticationService.validateAuthToken(localStorage.getItem('jwt_token')).subscribe( data => this.test = data );` –  Feb 03 '19 at 19:49
  • @AJT_82 - That looks like it; thank you so much bud! I'm not sure why I didn't know that was the place to do it--I kept trying to subscribe on the http.post side. Really appreciate you taking all the time to help through my ignorance. Success!: `{"code":"jwt_auth_valid_token","data":{"status":200}}` . Think I need to go re-read on Observables yet again. –  Feb 03 '19 at 19:51
  • Yes, well if you were trying to subscribe in the service (if you mean that with `trying to subscribe on the http.post side`) would mean that you would then return a **subscription**. Just remember that your data is inside the subscribe block :) – AT82 Feb 03 '19 at 19:56
  • No problem, glad we got it sorted out. Yes, dig into documentation to really get a good grip on the things! Good luck and happy coding! :) – AT82 Feb 03 '19 at 19:57
  • @AJT_82 - Yup, that is correct--and I did get a `subscription` when trying that, which is why I took it back off. =) Again, thank you for the help and tips! DM me your PayPal info or something; would love to buy you a coffee in thanks. –  Feb 03 '19 at 19:57

1 Answers1

1

Answer solved by @AJT_82 in the comments.

Issue wasn't with the line of the function I posted, but rather my call to the function on another page--where I did not subscribe.

My original this.test = this.authenticationService.validateAuthToken(localStorage.getItem('jwt_token'));

Needed to subscribe and actually get the data to be assigned.. instead of just keeping trying to assign the Observable back to my this.test variable like a doofus: this.authenticationService.validateAuthToken(localStorage.getItem('jwt_token')).subscribe( data => this.test = data );

  • Also forgot to mention that... if you do not want to use `subscribe()` you can also use the `async` pipe in template, but you'll run into that when digging deeper in the mysteries of Angular :) I prefer subscribing though and never found a liking for the async pipe. But that is my personal opinion ;) Many others like it! – AT82 Feb 03 '19 at 20:08
  • Since I can't see your code, I don't know exactly what is going on, but I guess it has to do with the fact that this is asynchronous. This should in that case help you: https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular2 – AT82 Feb 03 '19 at 20:31
  • @AJT_82 - Ack; sorry for pulling you back--I managed to figure it out just before you posted =) Thanks again! I will leave you alone now! haha; enjoy your evening! –  Feb 03 '19 at 20:33