简体   繁体   English

获取意外令牌:邮递员发布请求

[英]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 : 我想向我的REST API发出POST请求,但是我收到REST API是在春季制作的错误消息,我有一个意外的令牌:

POST Request Call : POST请求呼叫:

http://localhost/users/load

Java Code: Java代码:

@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: JSON数据:

{
    "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 请求主体必须在主体选项卡中,然后选择raw,然后将您粘贴json并在headers选项卡中添加header content-typeapplication/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. “测试”选项卡用于编写JavaScript代码以断言主体是否是我期望的以测试我的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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