How to pass two different objects to a POST request in postman to test RESTful API?
Your ClientData class is
@Data
public class ClientData {
private Client client;
private ClientAddress address;
}
So you request payload should look like
{
"client":{
"name": "Bilbo",
"email": "[email protected]"
},
"address":{
"streetName": "Central Street",
"streetNumber": "31"
}
}
Notice address instead of clientAddress. Also you don't need to pass it as array it will be just a json object (not [])
Mamun
Updated on August 02, 2022Comments
-
Mamun almost 2 years
This is actually a two part question, one is, how to pass two JSON objects, anther one is how to create object from two different classand pass that object to a POST method.
I am trying to test a POST method of RESTful API by passing two different objects in postman. I could successfully test only a POST method with one object, but when I am trying to send two different object to POST method where two different objects , postman say's bad request.
Here is my two entity class: First one is Cleint class:
import lombok.Data; import org.zevgma.api.entities.user.User; import javax.persistence.*; import java.util.Date; @Data @Entity @Table(name = "client") public class Client { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_pk_generator") @SequenceGenerator(name = "user_pk_generator", sequenceName = "users_user_id_seq", allocationSize = 1) @Column(name = "id", unique = true, nullable = false, columnDefinition = "serial") private int id; @Column(name = "name", nullable = false) private String name; @Column(nullable = false) private String email; @Column(nullable = false) private Date userDateCreate; }
Second one is about ClientAddress:
import lombok.Data; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Data @Entity public class ClientAddress { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private int streetID; private String streetName; private int streetNumber; }
I have tried different POST request with JSON objets or JSON nested threads in Stackoverflow and youtube, but could not find my solution. Spceially I have tried instructions from these threads:
- https://it.mathworks.com/help/thingspeak/bulkwritejsondata.html
- Post Multiple JSON Objects Simultaneously with Express and Postman
Here is the POST method of RESTful API which one I want to test:
@PostMapping(value = "/api/employees", consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<String> createClient(@RequestBody Client client, @RequestBody ClientAddress clientAddress){ clientService.save(client); clientAddressService.save(clientAddress); return new ResponseEntity<>(client.getName() + " and "+ clientAddress.getStreetName() + " successfully added", HttpStatus.OK); }
So far I have tried with this JSON object:
[ { "name": "Bilbo", "email": "[email protected]" }, { "streetName": "Central Street", "streetNumber": "31" } ]
[ { "client":{ "name": "Bilbo", "email": "[email protected]" }, "clientAddress":{ "streetName": "Central Street", "streetNumber": "31" } } ]
By Pre-Request Script:
//Pre-request Script var client = { "name": "Bilbo", "email": "[email protected]" } var address = { "streetName": "New Street", "streetNumber": "3" } pm.environment.set("client", JSON.stringify(client)); pm.environment.set("address", JSON.stringify(address)); //Body [ {{client}},{{address}} ]
Now the second part of questions: What if I want to create a new object, ClientData from the above two mentioned class, Client and ClientAddress and pass this ClientData object to a POST method, how to do it.
Here is the ClientData class:
import lombok.Data;
@Data public class ClientData { private Client client; private ClientAddress address; }
And here is the POST method where I want to pass a ClientData object:
@PostMapping(value = "/api/employees", consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<String> createClient(@RequestBody ClientData clientData){ Client client = clientData.getClient(); clientService.save(client); ClientAddress clientAddress = clientData.getAddress(); clientAddressService.save(clientAddress); return new ResponseEntity<>(client.getName() + " and "+ clientAddress.getStreetName() + " successfully added", HttpStatus.OK); }
To test method in postman I should create these complex ClientData JSON object and how to pass the it?