Search code examples
javaangularservletshttprequestangular-httpclient

Sending Request from Angular to Java Servlet


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?


Solution

  • 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