0

I have an angular app and a java servlet backend. what I need to do is send httpRequest from angular to backend. I am using httpClient to do that.
MyService.ts

import { Injectable, EventEmitter } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class MyServices {

  constructor(private httpClient: HttpClient) {}
  public getTables(num: number) {
    return this.httpClient.post("url/to/backend",JSON.stringify({number: num}));
  }
}

and in my servlet I receive the request but when I use requset.getParameter to get the number value it returns null. I'm sure when I read the request body it returns:

"{"number":3}"

when I use request.getParametersName it returns null again. does anyone know what I am doing wrong? and am I using the right tool for sending an httpRequest?

3
  • why do you stringify the body? Commented Jan 24, 2018 at 12:59
  • @mast3rd3mon I tried to do it without stringifying the body but it made no difference. Commented Jan 24, 2018 at 13:00
  • Could you solve it? I'm having the exact same problem, with the exact same situation. Reading the body through getReader() shows the JSON, but with getParameter() I don't seem to be able to get them. Thanks. Commented Jan 17, 2019 at 19:36

2 Answers 2

1

Try swapping the code to return this.httpClient.post("url/to/backend",{'number': num}); as you shouldnt stringify the body if the web handler expects a json body

Sign up to request clarification or add additional context in comments.

7 Comments

have you tried logging out the whole body? instead of just what the value is?
I tried that. I can log out the whole body with getReader(). as I said in the original question it logs "{"number":3"}" . I can use that but I don't think it's very good practice.
I haven't tried that. do you mean parsing it back using Gson?
well you said the logged value has quotes around it which suggests its a string, not json
my best guess is either the app isnt rebuilding properly when you remove the stringify or the java server code forces it to be a string
|
1

Not an expert in this, but when you are sending a request in post method the data is in body. In your servlet you are checking it in request parameter. I think you have to check in request body. I use spring framework than servlet, so can't provide exact code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.