2014-01-24 61 views
4

我在RestSharp的RestClient中使用POST方法和JSON修补程序操作(请参阅RFC:http://tools.ietf.org/html/rfc6902)有问题。 AddBody()包含这样的内容:restclient是否支持json-patch?

request.AddBody(new { op = "add", path = "/Resident", value = "32432" }); 

它出错了。我不知道如何通过json-patch在身体中的操作。我尽我所能去尝试。有没有解决这个问题的方法?

+0

您是否尝试过使用参数,而不是AddBody? – Prix

+0

看来我已经克服了这个问题,但问题仍然有效,因为我的错误描述已经改变。错误描述如下:{“type”:“error”,“status”:415,“code”:“unsupported_media_type”,“help_url”:“http:\/\/\ /#errors”,“message” :“Content-Type必须是应用程序\/json-patch + json”,“request_id”:“1953d8ac6”}我得到unsupported_media_type错误,并在restclient.execute()方法中引发错误。有没有办法解决这个问题? – user3231144

+0

Prix,是的,我曾尝试参数,但没有奏效,但无论如何,我现在能够在体内传递操作,但问题与json-patch有关。看来execute()方法不支持json-patch响应。有任何想法吗? – user3231144

回答

1

这个工作对我来说:

var request = new RestRequest(myEndpoint, Method.PATCH); 
request.AddHeader("Content-Type", "application/json-patch+json"); 
request.RequestFormat = DataFormat.Json; 
request.AddBody(
    new 
    { 
     op = "add", 
     path = "/Resident", 
     value = "32432" 
}); 

request.Parameters.FirstOrDefault(p => p.Type == ParameterType.RequestBody).Name = "application/json-patch+json"; 

var response = restClient.Execute(request); 
1

这是斯科特的答案的改进版本。我不喜欢查询参数和RestSharp提供了一种直接与AddParameter设置名称

var request = new RestRequest(myEndpoint, Method.PATCH); 
request.AddHeader("Content-Type", "application/json-patch+json"); 
request.RequestFormat = DataFormat.Json; 
var body = new 
{ 
    op = "add", 
    path = "/Resident", 
    value = "32432" 
} 
request.AddParameter("application/json-patch+json", body, ParameterType.RequestBody); 

var response = restClient.Execute(request);