JSON API .Net Core 放置和补丁示例

JSON API .Net Core Put and Patch Examples

我正在使用 json:api specification from github repo {json:api} 测试 dotnet 核心的样板库。当我从邮递员发送时,GET(有或没有查询)、POST 和 DELETE 的端点按预期工作。但是我找不到使用 PUT 或 PATCH 更改现有资源的工作示例。当我发送带有数据的补丁请求时,它返回响应“200 OK”,但它在数据库中没有改变。以下是我的请求和回复。

  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

如何更新数据?

我在文档中发现,对于不同的 api 调用,需要包含以下两个 headers,而 PATCH 的 body 请求也不同。

"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"
       } }

看完了JSONAPI和OData的规范文档后,我最终做出了决定。为了更好地理解我自己的代码,我将坚持使用我自己的格式,并且我推荐将 Swagger 用于 Api 文档。如果规格不符合我的要求,即使人们告诉它是标准,那也没有意义。