Method.Put 上的 RestSharp 奇怪错误
RestSharp weird error on Method.Put
我正在尝试使用 RestSharp 更新资源。 API 工作良好,因为在另一个应用程序中使用,所以这排除了一些路由问题或任何问题在我这边而不是在 API 一个。
总之。我目前的情况是我想更新位于 host/api/resource/id
的特定资源
这是我当前在 DataProvider
层中的代码
public override bool Update(string resource, Dictionary<string, object> properties)
{
this.request = new RestRequest(resource + "/{id}", Method.PUT);
for (int i = 0; i < properties.Count; ++i)
{
KeyValuePair<string, object> kvp = properties.ElementAt(i);
if (kvp.Key != "id")
this.request.AddParameter(kvp.Key, kvp.Value, ParameterType.GetOrPost);
else
this.request.AddParameter(kvp.Key, kvp.Value, ParameterType.UrlSegment);
}
var response = this.CallApi();
// ... other stuff
}
这段代码只是根据方法从外部接收到的字典创建请求和正确的参数,然后调用 CallApi()
方法,即 this
private IRestResponse CallApi()
{
var client = new RestClient(BaseUrl);
var response = client.Execute(this.request);
if(response.ErrorException != null)
{
// Response has some error!
// ... other stuff
}
if(response.StatusCode != System.Net.HttpStatusCode.OK)
{
// Response received something different from HTTP status code OK
// ... other stuff
}
return response;
}
CallApi 完美适用于所有其他调用,例如 GET、POST、DELETE 甚至 PATCH,但是当我尝试将它与 Update 一起使用并因此使用 PUT 时,从 client.Execute(this.request)
收到的响应是 405 方法不允许。
稍微调试后我发现响应有一个 ResponseUri
只有 host
字符串而不是 host/api/resource/id
这似乎是由
this.request = new RestRequest(resource + "/{id}", Method.PUT);
事实上,如果我删除 /{id}
部分,RequestUri 的正确形式是 host/api/resource
,当然没有 id,无论如何这是错误的,因为我需要 id :-/
有人知道为什么会这样吗?
问题出在新实例的反斜杠上。只需从 /{id}
中删除反斜杠即可
我正在尝试使用 RestSharp 更新资源。 API 工作良好,因为在另一个应用程序中使用,所以这排除了一些路由问题或任何问题在我这边而不是在 API 一个。
总之。我目前的情况是我想更新位于 host/api/resource/id
这是我当前在 DataProvider
层中的代码
public override bool Update(string resource, Dictionary<string, object> properties)
{
this.request = new RestRequest(resource + "/{id}", Method.PUT);
for (int i = 0; i < properties.Count; ++i)
{
KeyValuePair<string, object> kvp = properties.ElementAt(i);
if (kvp.Key != "id")
this.request.AddParameter(kvp.Key, kvp.Value, ParameterType.GetOrPost);
else
this.request.AddParameter(kvp.Key, kvp.Value, ParameterType.UrlSegment);
}
var response = this.CallApi();
// ... other stuff
}
这段代码只是根据方法从外部接收到的字典创建请求和正确的参数,然后调用 CallApi()
方法,即 this
private IRestResponse CallApi()
{
var client = new RestClient(BaseUrl);
var response = client.Execute(this.request);
if(response.ErrorException != null)
{
// Response has some error!
// ... other stuff
}
if(response.StatusCode != System.Net.HttpStatusCode.OK)
{
// Response received something different from HTTP status code OK
// ... other stuff
}
return response;
}
CallApi 完美适用于所有其他调用,例如 GET、POST、DELETE 甚至 PATCH,但是当我尝试将它与 Update 一起使用并因此使用 PUT 时,从 client.Execute(this.request)
收到的响应是 405 方法不允许。
稍微调试后我发现响应有一个 ResponseUri
只有 host
字符串而不是 host/api/resource/id
这似乎是由
this.request = new RestRequest(resource + "/{id}", Method.PUT);
事实上,如果我删除 /{id}
部分,RequestUri 的正确形式是 host/api/resource
,当然没有 id,无论如何这是错误的,因为我需要 id :-/
有人知道为什么会这样吗?
问题出在新实例的反斜杠上。只需从 /{id}
中删除反斜杠即可