0

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.

2
  • Do you subscribe to your POST request? Commented Aug 25, 2019 at 6:23
  • @igor_c Yes I did. var opost = new Posts(); opost.id=3; opost.title='Dove Tail'; this._freeApiService.post(opost) .subscribe ( data=> { this.objposts = data; } ); Commented Aug 25, 2019 at 7:12

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. but then I'll have to run two servers simultaneously. One for json-server and the other for 'npm start'. This will be a little dicey when i deploy the app on heroku. Any solution to this ? please
You can use concurrently to run multiple commands in the same npm script. You also don't need to "run" an Angular server when deploying, unless you're using SSR. You simply deploy the production build as static files.

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.