Tanzeel
Tanzeel

Reputation: 4998

POST data to json using my-json-server through Angular

I am trying to perform two simple tasks. GET and POST with a free REST api i.e Jsonplaceholder using angular. I have a json db.json uploaded on my github account here which is very simple. It contains id and name of the book i.e. title

{
    "myitems": [
        {
            "id": 1,
            "title": "Bike Servicing System"
        },
        {
            "id": 2,
            "title": "Easy Base"
        }
    ],
    "profile": {
        "name": "typicode"
    }
}

I am using a free REST api service JSONPlaceholder. They call it a "Fake Online REST API for Testing and Prototyping". Anyways, my GET request works perfectly. Click here to see GET response. But why I am unable to put some further data to that json using POST. Please see if there is something wrong with my code.

post(opost: Posts): Observable<any> {
    return this.httpclient.post("https://my-json-server.typicode.com/tmtanzeel/test-json-server/myitems", opost);
}

Where opost is an object of type Posts which is a class.

export class Posts {
    id: number;
    title: string;
}

I am not getting any error. Here I have put the entire angular project on github. And I was following this youtube tutorial. Please help me.

Upvotes: 0

Views: 2362

Answers (1)

Will Alexander
Will Alexander

Reputation: 3571

From the JSONPlaceholder docs:

To be able to provide a free service to as many people as possible during this phase, the project comes with a few limits:

  • Changes are faked and aren't persisted (just like JSONPlaceholder)

POST requests don't persist anything to the server. This makes sense, as it would mean requiring file write access to a server you don't own.

If you want a quick, local JSON server, try the json-server package (the basis for JSONPlaceholder:

https://github.com/typicode/json-server

Upvotes: 2

Related Questions