Florin Simion
Florin Simion

Reputation: 458

Customize the POST request in Angular 2

I'm creating a POST request, but I also need to specify the "type link relation".So the body should look like this:

  {
                "_links":{
                    "type":{
                        "href":"http://example.co.uk/rest/type/node/article"
                    }
                },
                "title":[{"value": "Blog title"}],
                "body":[{"value": "Body content"}]
            }

So far I'm getting the title and the body from user input (a form), but I don't know how to add this new object to my request.

This is my service:

  createBlog(blog: Blog): Observable<any>{
          let url = this.API_URL + "entity/node";
          return this.http.post(url, blog,  {headers:this.headers}).map(res => res.json()).catch(err => {

            return Observable.throw(err);
          });
        }

With this I get a bad request

400 Bad bequest
Response {_body: "{"error":"The type link relation must be specified."}", status: 400, ok: false, statusText: "Bad Request", headers: Headers…}

Any hints?

Upvotes: 0

Views: 195

Answers (1)

Victor Ivens
Victor Ivens

Reputation: 2279

You should probably expand the Blog object like this:

createBlog(blog: Blog): Observable<any>{
  blog._links = {type : { href: 'http://example.co.uk/rest/type/node/article' } };
  ....
}

And for that to work you will need to change the Blog model to contain a _link property of the type Object.

export class Blog {
    _links: Object;
    title: Array<Object>;
    ...
}

Upvotes: 1

Related Questions