最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does Postman throw a 400 status for this POST request with a pre-request script? - Stack Overflow

programmeradmin0浏览0评论

I'm working on an API REST with SpringBoot and I'm trying to create multiples users from Postman with a Pre-request script, but I get a 400 status code even when the users are save in the DataBase

"trace": ".springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public .springframework.http.ResponseEntity<library_management_system.entities.User>

This is the script:

let data = [{
    "name": "Alex M",
    "email": "[email protected]",
    "password": "ASF2354SDF"
},
{
    "name": "Lucas M",
    "email": "[email protected]",
    "password": "FGSASD123"
},
{
    "name": "Jenifer L",
    "email": "[email protected]",
    "password": "531234asd"
}
];

data.forEach(item => {
    pm.sendRequest({
        url: "http://localhost:8080/library/users",
        method: "POST",
        header: { "Content-Type": "application/json" },
        body: {
            mode: "raw",
            raw: JSON.stringify(item)
        }
    }, (err, res) => {
        if (err) {
            console.error("Error:", err);
        } else {
            console.log("Response:", res.json());
        }
    });
});

This is the POST method:

@PostMapping
    public ResponseEntity<User> create(@RequestBody User user) {
        return   ResponseEntity.status(HttpStatus.CREATED).body(userService.saveUser(user));
    }

User Class:

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @Column(nullable = false, unique = true)
    private String email;

    private String password;
}

What I have tried:

  • I have added the Content-Type key and its value (application/json) in the headers on Postman and it keeps showing the same status.

  • I changed "header" to "headers" in the script

  • Changed the body to

    body: JSON.stringify(item)
    
    

    I also checked the database and could see that the users are being saved, but I do not understand the 400 code status.

I'm working on an API REST with SpringBoot and I'm trying to create multiples users from Postman with a Pre-request script, but I get a 400 status code even when the users are save in the DataBase

"trace": ".springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public .springframework.http.ResponseEntity<library_management_system.entities.User>

This is the script:

let data = [{
    "name": "Alex M",
    "email": "[email protected]",
    "password": "ASF2354SDF"
},
{
    "name": "Lucas M",
    "email": "[email protected]",
    "password": "FGSASD123"
},
{
    "name": "Jenifer L",
    "email": "[email protected]",
    "password": "531234asd"
}
];

data.forEach(item => {
    pm.sendRequest({
        url: "http://localhost:8080/library/users",
        method: "POST",
        header: { "Content-Type": "application/json" },
        body: {
            mode: "raw",
            raw: JSON.stringify(item)
        }
    }, (err, res) => {
        if (err) {
            console.error("Error:", err);
        } else {
            console.log("Response:", res.json());
        }
    });
});

This is the POST method:

@PostMapping
    public ResponseEntity<User> create(@RequestBody User user) {
        return   ResponseEntity.status(HttpStatus.CREATED).body(userService.saveUser(user));
    }

User Class:

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @Column(nullable = false, unique = true)
    private String email;

    private String password;
}

What I have tried:

  • I have added the Content-Type key and its value (application/json) in the headers on Postman and it keeps showing the same status.

  • I changed "header" to "headers" in the script

  • Changed the body to

    body: JSON.stringify(item)
    
    

    I also checked the database and could see that the users are being saved, but I do not understand the 400 code status.

Share Improve this question asked Mar 11 at 16:21 Neoly MorenoNeoly Moreno 231 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Pre-request Script: You can use pre-request scripts in Postman to run JavaScript before a request runs.

If you provide the pre request script, every request you make runs automatically after executing the pre-request script. In your case, you have provided a pre-request script; however, your POST request body is empty. This is the reason for the error.

.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing

For every POST request, you must provide a request body in addition to the pre-request script (pre-request script is optional)

发布评论

评论列表(0)

  1. 暂无评论