Elad Benda Elad Benda - 1 year ago 110
TypeScript Question

how can i send 2 object in angular2 post request

I have this code:

client side (angular2)

saveConfig(configType: ConfigTypes, gasConfigModel: GasConfigModel): any {
console.info("sending post request");
let headers = new Headers({
'Content-Type': 'application/json'


return this.http
.post(this.url, formParamString, ??, {headers: headers})
.map(res => res.json())
data => {
console.info("next: ");
err => console.error(err)


server side:

@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public void saveConfig(MyObj my object, CountryGasStationConfig countryGasStationConfig) throws Exception {....}

I know how to send one object in post, but how can i send 2 object in post request?

Answer Source

Since you are using Json as your media, it is technically impossible to achieve this, as two separate arbitrary objects in a single document would be invalid Json.

However, a potential solution here would be to create an object that wraps these two objects.

Request Object

public SaveConfigRequest {
    private MyObj myObject;
    private CountryGasStationConfig countryGasStationConfig;

    //getters and setters

New Method Signature

public void saveConfig(SaveConfigRequest request)

Example Json Document

  "myObject": {...},
  "countryGasStationConfig": {...}