2

I have a REST API that is accepting two parameters that are username and password and giving the result as true or false on the basis of user input at front end. I am facing some issue in calling the REST API to my angular 8 code.

SPRING REST API

@RequestMapping(value = "/checkData", method = RequestMethod.POST)
public String Authentication(@RequestParam("username") String username, @RequestParam("password") String password) {

}

I am trying to access the given API through my angular 8 app through services and defined the service as follows :

AuthLoginInfo.ts

export class AuthLoginInfo {
    username: string;
    password: string;
 
    constructor(username: string, password: string) {
        this.username = username;
        this.password = password;
    }
}
checkLogin(userInfo : AuthLoginInfo){
     return this.http.post(`${API_URL}/APP/checkData/${userInfo.username}/
       ${userInfo.password}`,{},{responseType: 'text'})
  }

But when I am running my app I am not getting the params in proper format. Can anyone tell me how to define the request params in HTTP POST API ?

Thank you in advance for your suggestions.

5
  • Is the url you are calling something like api_url/APP/checkData/yourUsername/yourPassword ? Commented Feb 5, 2021 at 9:59
  • No, I want this to be appeared as localhost:8080/APP/checkdata?password=abc&username=xyz Commented Feb 5, 2021 at 10:06
  • Then what you are doing is correct. Are you able to call the endpoint with postman? Commented Feb 5, 2021 at 10:12
  • Yes, I am able to call it through postman but when I am hitting the same to the Angular UI it is not appearing correct. It seems to me something like this : localhost:8080/APP/checkData/abc/%20%20%20%20%20%20%20xyz Commented Feb 5, 2021 at 10:17
  • I am getting 404 not found as the URL is not coming in proper format Commented Feb 5, 2021 at 10:18

1 Answer 1

1

The way you send them from the frontend, they are considered path parameters not query parameters. Configure your backend in the same way

@RequestMapping(value = "/checkData/{username}/{password}", method = RequestMethod.POST)
public String Authentication(@PathParam("username") String username, @PathParam("password") String password) {

}

In case that you want to work with your existing code and you don't want to change your backend, then you must adjust your frontend. As your backend is right now it expects query parameters in the url, so you need to send those in the frontend

checkLogin(userInfo : AuthLoginInfo){
     return this.http.post(`${API_URL}/APP/checkData?username=${userInfo.username}&
       password=${userInfo.password}`,{},{responseType: 'text'})
  }
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, Boug for response. Can you suggest something on front end side ? How to access the @RequestParam on the front end side as I am using spring in the back end and fetching the query parameters.
On the frontend you don't access any RequestParam. You receive only a response which is a text. That is what you will receive and take care in the frontend
I mean to say that I am fetching the username and password from the DB. I had used @RequestParam as I am fetching the query parameters. The given data is not coming from the path URI.
Can you suggest how to use HttpParams with post method at UI side?
As you have your this.http.post(`${API_URL}.... in your UI it should work
|

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.