Httpclient response null when calling Java Servlet

Niels Van Haren

I'm having some difficulty figuring out why im getting a null response in angular when i try to do a post to a httpservlet (java) in a WAS environment.

What im trying to do is call an api to login. I would like to receive the response headers, but im getting a null response, making it impossible to do anything with the response.

I tried to send my parameters as form data with the content-type application/x-www-form-urlencoded;charset=UTF-8 and as query parameters with no content-type defined.

In both situations im able to login and im getting a 200 response with header information in my browser.

this is my call

    return this.http.post(this.contextRoot.getHost() + 'osa-kantoor-ws/api/auth/login?' + param1 + "&" + param2,null, this.httpOptions).map((res : Response) => {
      localStorage.setItem('currentUser', JSON.stringify(username));
      return res;
    });

My header info now is

  httpOptions = {
    headers: new HttpHeaders({'Content-Type' : 'application/json', 'Accept':  'application/text'}),
    params: {}
  };

the localstorage item is set, but the response information is null.

this is my subscribe

 login() {
    this.loginService.login(this.loginForm.value.userName,this.loginForm.value.password)
      .subscribe(
        data => {
          console.log(data);
          this.router.navigate([this.returnUrl]);
        },
        error => {
          console.log(error);
          this.loginfail = true;
        });
  }

When im checking the browser info on my response header i get the following info

enter image description here

can anyone tell me what im missing?

below is the original method

 login(username: string, password: string) {
    let param1: string = encodeURIComponent('userName') + '=' + encodeURIComponent(username);
    let param2: string = encodeURIComponent('password') + '=' + encodeURIComponent(password);
    let formParams: string = param1 + '&' + param2;
    return this.http.post(this.contextRoot.getHost() + 'osa-kantoor-ws/api/auth/login', formParams, this.httpOptions).map(response=> {
      localStorage.setItem('currentUser', JSON.stringify(username));
      console.log(response);
      return response;
    });
  }

Niels Van Haren

thank you AJT_82,

adding observe: 'response' to the config is the solution.

response is now what i expect. the default response is observe: 'body' apparently.

My HTTP config is now

  httpOptions = {
    headers: new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8','Accept':  'application/xml'}),
    observe: 'response'
  };

and my response is now

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting a null response when calling a php site function from java in android

From Dev

httpclient response is null when API returns a 204 status code

From Dev

Unexpected response when using HttpClient

From Java

calling a java servlet from javascript

From Java

Calling a Servlet from a Java application

From Dev

Java Servlet Null attributes

From Dev

jsp session implicit object is throwing null pointer exception when calling it from servlet

From Dev

response text java servlet in ajax

From Dev

HttpClient Get request returning null response

From Dev

Angular HttpClient response is null but network tools show response

From Dev

Error when accessing httpClient data response properties

From

Logging request/response messages when using HttpClient

From Dev

Calling Firebird Stored procedure from Java Servlet

From Dev

Java Servlet Method Parameters, response, and thread safety

From Java

Java 9 HttpClient: Correctly handle async response

From Dev

Receive null when calling on a class

From Dev

How to fix unreadable XML response when calling a SOAP web service with Java?

From Dev

Incorrect Response when calling an action concurrenct

From Dev

"No shop provided" response when calling shopify api

From Dev

Error 405 when on java servlet

From Dev

Calling main method with null in Java

From Dev

Mock HttpClient.SendAsync to return response with Content not null

From Dev

When calling Servlet I get a 500 - Internal Server Error

From Dev

Typescript with Angular HttpClient ErrorType ... is not a function when calling class method

From Dev

System.Net.HttpClient - Unexplained timeout when calling GetAsync

From Dev

Angular HttpClient issue when calling Bing Maps Locations REST methods

From Dev

Prevent httpclient from adding server url when calling presigned url

From Dev

JSP getting null values from Java servlet

From Dev

java.lang.NumberFormatException: null Error in servlet

Related Related

  1. 1

    Getting a null response when calling a php site function from java in android

  2. 2

    httpclient response is null when API returns a 204 status code

  3. 3

    Unexpected response when using HttpClient

  4. 4

    calling a java servlet from javascript

  5. 5

    Calling a Servlet from a Java application

  6. 6

    Java Servlet Null attributes

  7. 7

    jsp session implicit object is throwing null pointer exception when calling it from servlet

  8. 8

    response text java servlet in ajax

  9. 9

    HttpClient Get request returning null response

  10. 10

    Angular HttpClient response is null but network tools show response

  11. 11

    Error when accessing httpClient data response properties

  12. 12

    Logging request/response messages when using HttpClient

  13. 13

    Calling Firebird Stored procedure from Java Servlet

  14. 14

    Java Servlet Method Parameters, response, and thread safety

  15. 15

    Java 9 HttpClient: Correctly handle async response

  16. 16

    Receive null when calling on a class

  17. 17

    How to fix unreadable XML response when calling a SOAP web service with Java?

  18. 18

    Incorrect Response when calling an action concurrenct

  19. 19

    "No shop provided" response when calling shopify api

  20. 20

    Error 405 when on java servlet

  21. 21

    Calling main method with null in Java

  22. 22

    Mock HttpClient.SendAsync to return response with Content not null

  23. 23

    When calling Servlet I get a 500 - Internal Server Error

  24. 24

    Typescript with Angular HttpClient ErrorType ... is not a function when calling class method

  25. 25

    System.Net.HttpClient - Unexplained timeout when calling GetAsync

  26. 26

    Angular HttpClient issue when calling Bing Maps Locations REST methods

  27. 27

    Prevent httpclient from adding server url when calling presigned url

  28. 28

    JSP getting null values from Java servlet

  29. 29

    java.lang.NumberFormatException: null Error in servlet

HotTag

Archive