Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

How to define @RequestParams of spring POST API to Http post method of Angular?

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.

question from:https://stackoverflow.com/questions/66061111/how-to-define-requestparams-of-spring-post-api-to-http-post-method-of-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'})
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...