Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman form-data sending complex object with file

How to send (or maybe it's not possible?) complex object with file in Postman

My object:

class Client {
    private String clientName;
    private Platform platform;
}

class Platform {
    private String android;
    private String ios;
}

My Controller class:

@PostMapping(value = "/evaluate", produces = "application/json")
public ResponseEntity<ServerResponse> sendEvaluateForm(Client client,
        @RequestParam(value = "files", required = false) MultipartFile files)
{
    return new ResponseEntity<>(HttpStatus.OK);
}

That's how I am sending request in postman: enter image description here

It work's when I pass "clientName" which is basic field in Client. But I have no idea, how to pass Platform object. I tried to pass in key: platform and in value: {"android" : "asd", "ios" : "xxx"} But i only got BadRequest(400)

like image 300
Moler Avatar asked Aug 10 '18 08:08

Moler


Video Answer


2 Answers

With Postman you can build a request containing Files and Object at the same time.

Result expected as backend req.body:

{ street: '69 Pinapple street', city: 'Apple', zip: 6969, country: 'Pen' }

Postman ScreenShoot

like image 182
L_ObeZ Avatar answered Sep 30 '22 22:09

L_ObeZ


You can try send your client data as a plain string and parse it on the controller side.

    @PostMapping(value = "/evaluate", produces = "application/json")
    public ResponseEntity<?> sendEvaluateForm(@RequestParam ("client") String client,
                                                               @RequestParam(value = "files", required = false) MultipartFile files) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        Client clientobject = mapper.readValue(client, Client.class);

        return ResponseEntity.ok().build();
    }

And the postman request:

enter image description here

And your POJO classes:

class Client {
    private String clientName;
    private Platform platform;

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public Platform getPlatform() {
        return platform;
    }

    public void setPlatform(Platform platform) {
        this.platform = platform;
    }
}

class Platform {
    private String android;
    private String ios;

    public String getAndroid() {
        return android;
    }

    public void setAndroid(String android) {
        this.android = android;
    }

    public String getIos() {
        return ios;
    }

    public void setIos(String ios) {
        this.ios = ios;
    }
}
like image 32
Marcin Bukowiecki Avatar answered Oct 01 '22 00:10

Marcin Bukowiecki