Mamun
Mamun

Reputation: 375

How to pass two different objects to a POST request in postman to test RESTful API?

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:

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?

Upvotes: 3

Views: 28250

Answers (2)

TruckDriver
TruckDriver

Reputation: 1456

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 [])

Upvotes: 2

Sivcan Singh
Sivcan Singh

Reputation: 1892

Instead of doing this in the body:

[{{client}}, {{address}}]

Replace it with this:

{{requestBody}}

Now, store the complete body in an environment variable. Remove the separate variables for client and address and just use a single variable.

So, update your pre-request script to something like this:

pm.environment.set("requestBody", JSON.stringify([client, address]));

Upvotes: 0

Related Questions