惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
N
News and Events Feed by Topic
C
Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
Scott Helme
Scott Helme
P
Palo Alto Networks Blog
S
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tor Project blog
量子位
G
Google Developers Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
NISL@THU
NISL@THU
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
AWS News Blog
AWS News Blog
爱范儿
爱范儿
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
Security Archives - TechRepublic
Security Archives - TechRepublic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
Cloudbric
Cloudbric
aimingoo的专栏
aimingoo的专栏
L
Lohrmann on Cybersecurity
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hacker News: Ask HN
Hacker News: Ask HN
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
S
Security @ Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
P
Proofpoint News Feed
V
V2EX
Martin Fowler
Martin Fowler
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Cloudflare Blog
SecWiki News
SecWiki News
罗磊的独立博客
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
The Last Watchdog
The Last Watchdog

博客园 - 青玄鸟

.NET 中优雅处理 Server-Sent Events 请求取消 vue3.0 + ts 实现上传工厂(oss与cos) Dapr 订阅者参数无法正确反序列化问题 .NET 代码整洁手册 Blazor项目通过docker和nginx部署为静态站点的步骤 Moq mock 方法返回null空指针异常 基于接口隔离原则的依赖注入实现 HttpClient with Stream HttpClient 基本使用 值对象的封装 只读集合类型属性实现 适配器模式 模板模式 最少知识原则 单例模式 抽象工厂 简单工厂、工厂方法、抽象工厂 工厂方法模式 使用 Visual Studio Code创建和执行T-SQL
HttpClient partial update
青玄鸟 · 2020-04-28 · via 博客园 - 青玄鸟

full update 与 partial update

partial update:发送需要对远程资源做的变更(集合)

full update:发送变更后的资源实体

Json Patch

Json Patch 是描述一个json文档的变化的一种格式。可以避免在文档的一部分发生变化时发送整个文档。Json Patch在结合Http Patch Method 使用时,允许部分更新Api资源。Json Patch文档用Json格式表示:

原始文档:

{
  "baz": "qux",
  "foo": "bar"
}

Json Path:

[
  { "op": "replace", "path": "/baz", "value": "boo" },
  { "op": "add", "path": "/hello", "value": ["world"] },
  { "op": "remove", "path": "/foo" }
]

更新后文档:

{
  "baz": "boo",
  "hello": ["world"]
}

Json Patch详情

实现

客户端

private async Task PatchResource(HttpClient httpClient)
        {
            var patchDoc = new JsonPatchDocument<DemoForUpdate>();
            patchDoc.Replace(d=>d.Title,"another title");
            patchDoc.Remove(d=>d.Description);

            var serializedChangeSet = JsonConvert.SerializeObject(patchDoc);

            var request = new HttpRequestMessage(HttpMethod.Patch, "api/demos/163");
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Content = new StringContent(serializedChangeSet);
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var response = await httpClient.SendAsync(request);
            response.EnsureSuccessStatusCode();

            var content =await response.Content.ReadAsStringAsync();
            var updateDemo = JsonConvert.DeserializeObject<Demo>(content);
        }

服务端

        [HttpPatch("{demoId}")]
        public async Task<IActionResult> PartiallyUpdateMovie(int demoId, 
            [FromBody] JsonPatchDocument<Models.DemoForUpdate> patchDoc)
        {
            var demoEntity = await _demosRepository.GetDemoAsync(demoId);
            if (demoEntity == null)
            {
                return NotFound();
            }

            var demoToPatch = Mapper.Map<Models.DemoForUpdate>(demoEntity );

            patchDoc.ApplyTo(demoToPatch, ModelState);
              
            if (!ModelState.IsValid)
            {
                return new UnprocessableEntityObjectResult(ModelState);
            }
            Mapper.Map(demoToPatch, demoEntity );

            _demosRepository.UpdateMovie(demoEntity);

            await _demosRepository.SaveChangesAsync();

            return Ok(_mapper.Map<Models.Demo>(demoEntity));
        }