简体   繁体   中英

Getting unexpected token : Postman post request

I'm want to make a POST request to my REST API but I'm getting the error that The REST API is made in Spring I have an unexpected token :

POST Request Call :

http://localhost/users/load

Java Code:

@PostMapping(value = "/load")
public User load(@RequestBody final User user){
    userRepository.save(user);
    return userRepository.findByName(user.getName());
}

User Class:

@Entity
public class User {
private Long id;
private String name;
private String username;
private String password;

@Id
@GeneratedValue
public Long getId(){
    return id;
}

public void setId(Long id){
    this.id = id;
}

public String getName(){
    return name;
}

public void setName(String name){
    this.name = name;
}

public String getUsername(){
    return username;
}

public void setUsername(String username){
    this.username = username;
}

public String setPassword(){
    return password;
}

public void getPassword(){
    this.password = password;
}
}

JSON data:

{
    "name": "John", 
    "username": "randomUsername", 
    "password": "Password123!"
}

Postman: 在此处输入图片说明

Error:

There was an error in evaluating the test script:  SyntaxError: Unexpected token :

I expected that the request should pass but it didn't

Because you are adding request body in tests tab,

Request body must be in body tab then select raw, and paste you json also add header in headers tab content-type : application/json

Please check this sample

Postman ref

Tests tab is for writing JavaScript code to assert if body is what i expected in order to test my app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM