0

I want to pass multiple parameters to http post method in Angular4. Here each parameter is JSON object. Can any one please explain how to pass the JSON parameters. For my following code Request Payload displaying as data=%5Bobject%20Object%5D&obj1=%5Bobject%20Object%5D.

 saveData(data : any): Promise<any> {

    let obj1 : any = {'p_id':'2'};
    let params = new URLSearchParams();
    params.append("data", data);
    params.append("obj1", obj1);

    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.post('urlContent', params, options).toPromise()
           .then(response => response.json())
               .catch(this.handleError);
}
5
  • Just try to encapsulate all data which you want to send with object in JSON structure and send it as one object data. Commented Oct 12, 2017 at 10:19
  • Do you mean as the post body? Path parameters? Query string? What is the API expecting? If you want a body, why are you passing them as search parameters? Commented Oct 12, 2017 at 10:20
  • @Ziyaddin Sadigov I tried with encapsulate, But two JSON object passing as one parameter. I want send to service as two parameters Commented Oct 12, 2017 at 10:21
  • @arjun it is better to send it as one unified object and then parse this data to two separate JSON objects in back-end side. Commented Oct 12, 2017 at 10:25
  • @ Ziyaddin Sadigov sorry, I dont have access to change back_end. Commented Oct 12, 2017 at 10:46

2 Answers 2

1

Instead of using the URLSearchParams(), I have used the array and passed the data. It works for me.

saveRtlList(data : any): Promise<any> {

        let obj1 : any = {'p_id':'2'};

        let savedata : Array<any> = [];
        savedata.push(JSON.stringify(data));
        savedata.push(obj1 );   
        let values : any = {} as any;  
        values.param = savedata;

        return this.http.post(url, {}, {params:vals}).toPromise()
               .then(response => response.json())
                   .catch(this.handleError);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

try this :

saveData(data : any): Promise<any> {

    let obj1 : any = {'p_id':'2'};
    let body= {obj1 ,data}; 

    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.post('urlContent', body, options).toPromise()
           .then(response => response.json())
               .catch(this.handleError);
}

1 Comment

@ Bougarfaoui El houcine, let body= {obj1 ,data}; because of encapsulation, two objects passing as a single object param. I need to pass as two individual object params, please share any other approaches..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.