1

I am trying to send two values from client to server side using HTTP post in MEAN. I can add one but when I try to add two values I am getting an error.

Here is my code:

var emailid = 'emailid=' + ctinvite.emp_email;
var phone= 'phone=' + ctinvite.emp_phone;
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post('http://localhost:3000/sendmail',emailid, {headers: headers})

I added emailid and it's working perfectly. Now I want to add phone along with emailid in HTTP Post.

3 Answers 3

2

You should be using a complex object as below

var emailid = 'emailid=' + ctinvite.emp_email;
var phone= 'phone=' + ctinvite.emp_phone;\
var email_phone ={
    emailid : emailid,
    phone :phone
}
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post('http://localhost:3000/sendmail',email_phone, {headers: headers})
Sign up to request clarification or add additional context in comments.

Comments

1

If you will to create an url with params like this:

http://url.com/user?emailid=12&phone=2234234234

than you should use HttpParams (or URLSearchParams for old (<4.3) Angular version). Here you can find a little bit more.

Here is an example based on your question data:

Angular v4.3+ with new HttpClientModule:

let httpParams = new HttpParams();
httpParams = httpParams.set('emailid', ctinvite.emp_email.toString());
httpParams = httpParams.set('phone', ctinvite.emp_phone.toString());
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post('http://localhost:3000/sendmail', null, {headers: headers, params: httpParams});

Angular v4.3 and older with old HttpModule:

let params = new URLSearchParams();
params.set('emailid', ctinvite.emp_email.toString());
params.set('phone', ctinvite.emp_phone.toString());
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post('http://localhost:3000/sendmail', null, {search: params, headers: headers});

If you will to send you data as FormData (that I see on your headers settings) so here is an example for that:

const formData = new FormData();
formData.append('emailid', ctinvite.emp_email.toString());
formData.append('phone', ctinvite.emp_phone.toString());
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post('http://localhost:3000/sendmail',formData, {headers: headers});

If you will to send you data as JSON object, so you should make it like this:

let jsonData = {
  emailid: ctinvite.emp_email,
  phone: ctinvite.emp_phone
}
headers.append('Content-Type', 'application/json');
this.http.post('http://localhost:3000/sendmail', JSON.stringify(jsonData), {headers: headers});

Comments

0

Use following code :

var emailid = 'emailid=' + ctinvite.emp_email;
var phone= 'phone=' + ctinvite.emp_phone;
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post(`http://localhost:3000/sendmail=${emailid}&phone=${phone}`, {headers: headers})

Hope it will help

3 Comments

adding query params in post is not advisable
rather you can wrap objects in single structure, and pass that object in the post request.
Can you send your api code then I can help you better. The best way to send data in post is to send in the body of the request. If you are getting your data in api in a single object then obviously you need to send an object in the body of the request.

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.