Aditya
Aditya

Reputation: 2536

How to send an object to http.post request?

My contact contains an object.I want to send to the http.post method.Below is my code, where should I put contact object as a parameter.Please give links also related to http.post API if any. Code:

createContacts(contact) {
    console.log('the contact inside subscribe function',contact);
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    this.http.post('http://localhost:8080/contacts',{headers : headers}).subscribe(res => {
      console.log('inside postmehtod of sub.function', res.json());//only objects

    });

  }

package.json

{
  "name": "contactlist",
  "version": "0.1.0",
  "description": "A sample Node.js app using Express 4",
  "engines": {
    "node": "5.9.1"
  },
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "devDependencies": {
    "mongoose": "^4.6.2",
    "body-parser": "^1.15.2",
    "cors": "^2.8.0",
    "del": "2.2.0",
    "express": "^4.14.0",
    "http": "0.0.0",
    "method-override": "^2.3.6",
    "morgan": "^1.7.0",
    "superlogin": "^0.6.1"
  }
}

Upvotes: 1

Views: 23420

Answers (1)

Tom
Tom

Reputation: 5121

Link to http post api as requested

Typescript post API: post(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response>

Example:

createContacts(contact) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    this.http.post('http://localhost:8080/contacts', contact, {headers : headers})
      .subscribe(res => {
         console.log('inside postmehtod of sub.function', res.json());//only objects
      })
  }

Upvotes: 4

Related Questions