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

I'm working on an API REST with SpringBoot and I'm trying to create multiples users from Post

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744782278a4593412.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信