简体   繁体   English

使用RestHeart删除HttpClient

[英]HttpClient delete with RestHeart

I have a problem on using RestHeart. 我在使用RestHeart时遇到问题。 I want to delete a specific document in MongoDB server and I confirmed the below command works fine in command prompt. 我想删除MongoDB服务器中的特定文档,并确认以下命令在命令提示符下工作正常。

http delete localhost:8080/mytest/users/56dda76daeb32b0860d909e1 if-match:56dda76daeb32b0860d909e2

The document was removed correctly and then I created a document in the same collection I wrote some C# code to remove the new created document. 正确删除了该文档,然后在同一集合中创建了一个文档,并编写了一些C#代码以删除新创建的文档。

public async void TryDeleteAsync() {
        using(var client = new HttpClient()) {
            client.BaseAddress = new Uri("http://127.0.0.1:8080/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));                
            client.DefaultRequestHeaders.IfMatch.Clear();
            client.DefaultRequestHeaders.IfMatch.Add(new EntityTagHeaderValue("\"56dda76daeb32b0860d909e5\""));

            HttpResponseMessage response = await client.DeleteAsync("/mytest/users/56dda76daeb32b0860d909e4");
            if(response.IsSuccessStatusCode) {
                var result = await response.Content.ReadAsStringAsync();
                Console.WriteLine(result);
            }
        }
    }

Document and ETag id are correct but I got response 412 precondtion failed message. 文档和ETag ID正确,但是我收到响应412预处理失败的消息。 What's wrong in this code? 这段代码有什么问题?

Thanks. 谢谢。

Error 412 - Precondition failed, means that the ETag does not match. 错误412-前提条件失败,表示ETag不匹配。 Looking at this code line: 看下面的代码行:

client.DefaultRequestHeaders.IfMatch.Add(new EntityTagHeaderValue("\"56dda76daeb32b0860d909e5\""));

I noticed the escaped quotes in the ETag value passed to the EntityTagHeaderValue constructor. 我注意到传递给EntityTagHeaderValue构造函数的ETag值中的转义引号。 Try with: 试试:

client.DefaultRequestHeaders.IfMatch.Add(new EntityTagHeaderValue("56dda76daeb32b0860d909e5"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM