Json-server: PUT/POST dismissing payload data

Created on 14 Oct 2017  路  2Comments  路  Source: typicode/json-server

I'm trying to get a toy project working with json-server at the backend.

The project code and a (rather short) description as to its goal can be found here: https://github.com/ob-ivan/DepartmentsEmployees

Follow the installation and running instructions, and you'll get to a page with two lines table show example IT department with id=1.

Now when you try to edit its name, window.fetch sends PUT request to json-server:

PUT /departments/1
{"id":1,"name":"ITasdasd"}

and the server returns 200. The issue is the response body which is:

{
  "id": 1
}

As I check db.json, it, too, has name field removed from IT department.

Also if you enter a name in the last line, POST request is sent instead to create a new entry, which is created almost successfully: it misses name field, too.

Where my name field is gone? Is it a misuse on my behalf or a bug in json-server?

Most helpful comment

Ok, it's just me. I missed this line in the docs:

A POST, PUT or PATCH request should include a Content-Type: application/json header to use the JSON in the request body. Otherwise it will result in a 200 OK but without changes being made to the data.

I'm closing this, but I'd suggest providing a live example in the docs to make it a bit more clear. The example may be as simple as:

window.fetch(
    "http://localhost:3000/posts/5",
    {
        method: "PUT",
        body: JSON.stringify({ title: "Check out a cool json-server example!" }),
        headers: {
            "Content-Type": "application/json" // <--- don't forget this!
        }
    }
)

All 2 comments

Ok, it's just me. I missed this line in the docs:

A POST, PUT or PATCH request should include a Content-Type: application/json header to use the JSON in the request body. Otherwise it will result in a 200 OK but without changes being made to the data.

I'm closing this, but I'd suggest providing a live example in the docs to make it a bit more clear. The example may be as simple as:

window.fetch(
    "http://localhost:3000/posts/5",
    {
        method: "PUT",
        body: JSON.stringify({ title: "Check out a cool json-server example!" }),
        headers: {
            "Content-Type": "application/json" // <--- don't forget this!
        }
    }
)

I set same configure. but, I have same error yet.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

boydenhartog picture boydenhartog  路  3Comments

0plus1 picture 0plus1  路  3Comments

sboudouk picture sboudouk  路  3Comments

sharpmachine picture sharpmachine  路  4Comments

eddieyanez picture eddieyanez  路  4Comments