phonemyatt
phonemyatt

Reputation: 1377

JSON API .Net Core Put and Patch Examples

I am testing boilerplate library for dotnet core with json:api specification from github repo {json:api}. The endpoints for GET (with or without query), POST & DELETE are working as expected when I send from postman. But I couldn't find working examples to change the existing resource with PUT or PATCH. When i send patch request with data, it give me back response "200 OK" but it didn't change in database. Below are my request and response.

  Request GET : http://localhost:5000/api/people -> 200 OK
  Response : [
  {
    "name": "Samuel",
    "articles": null,
    "id": 2,
    "stringId": "2"
  },
  {
    "name": "John",
    "articles": null,
    "id": 3,
    "stringId": "3"
  },
  {
    "name": "Robbin",
    "articles": null,
    "id": 4,
    "stringId": "4"
   } ]

  Request GET: http://localhost:5000/api/people/2 -> 200 OK
  Response : {
   "name": "Samuel",
   "articles": null,
   "id": 2,
   "stringId": "2" 
  }

  Request GET: http://localhost:5000/api/people/2?include=articles -> 200 OK
  Response : {
   "name": "Samuel",
   "articles": [],
   "id": 2,
   "stringId": "2" 
  }

  Request POST: http://localhost:5000/api/people -> 201 Created
  Request Body: {"name":"Samuel"}
  Response : {
   "name": "Samuel",
   "articles": null,
   "id": 2,
   "stringId": "2" 
  }

  Request DELETE: http://localhost:5000/api/people/2 -> 204 No Content

How can I update data?

Upvotes: 0

Views: 715

Answers (2)

phonemyatt
phonemyatt

Reputation: 1377

I made a final decision after reading specification documents of JSONAPI and OData. I will just stick to my own format for better understanding of my own code and I recommend Swagger for Api Documentation. It doesn't make sense if the spec doesn't meet my requirement even when people are telling it's the standard.

Upvotes: 2

phonemyatt
phonemyatt

Reputation: 1377

I found in documents that require to includes following two headers for different api calls and body request is also different for PATCH.

"Accept: application/vnd.api+json"       <--- This needs to put in header
"Content-Type: application/vnd.api+json" <--- This also needed.

Request PATCH: http://localhost:5000/api/people/3 -> 200 OK
// Request body becomes text, anybody knows how to format to JSON?
Request Body(Text): {
 "data": {
  "type": "people",
  "attributes": {
       "name": "John"
     }
   }
}

Response : {
      "data": {
         "attributes": {
        "name": "John"
    },
      "relationships": {
        "articles": {
            "links": {
                "self": 
            "http://localhost:5000/api/people/3/relationships/articles",
                "related": "http://localhost:5000/api/people/3/articles"
            }
        }
       },
          "type": "people",
          "id": "3"
       } }

Upvotes: 1

Related Questions