我想用 Put 命令调用端点。
在 Postman 中
我可以举个例子 https://example.com/customers/106
。然后我添加一个类型为 application/json
的主体(在 raw
下)。
当我将
这个主体放到端点时,我得到一个200 OK
。
我使用的端点需要两个自定义 header 和一个内容类型,这是我在 header 下制作的。所以我添加了三个 header :X-AppSecretToken
、X-AgreementGrantToken
和 Content-Type
(to application/json
).
在 RestSharp 中
这里我使用了以下内容。 putstr
与我在 Postman 中Put
完全相同:
var restclient = new RestSharp.RestClient("https://example.com");
var request = new RestRequest("/customers/" + customerId, Method.PUT);
request.AddHeader("X-AppSecretToken", systemToken);
request.AddHeader("X-AgreementGrantToken", userToken);
request.AddHeader("Accept", "application/json");
request.AddJsonBody(putstr);
var response = restclient.Execute(request);
现在,当我这样做时,我收到以下响应,这是我正在调用的 API 的自定义错误:
"{\"message\":\"Error converting value SOME STUFF}}\\\" to type 'eco.rest.Models.Customer'. Path '', line 1, position 605.\",\"errorCode\":\"E00500\",\"developerHint\":\"The JSON payload could not be parsed. The error message should give you a good indication of where the error is located. One common reason for this error is a missing '{' or a '}' in your payload.\",\"logId\":\"123fc6fb4964a141f612ae8ae7571446\",\"httpStatusCode\":400,\"logTime\":\"2018-05-20T21:56:56\"}"
如何修复?
通常,我绝不会问这个问题。如果有人问,我会说:打开 Fiddler 或类似工具,看看请求有何不同。
我遇到了一些麻烦,因为它是 HTTPS。
当我调试我的代码时,我根本看不到 Fiddler 中的调用。我也安装了 Charles,但也不走运。不确定是什么问题。
但是,我认为读到这篇文章的人可能会想出这个问题。我自己的假设是我可能以错误的方式添加了 header ,JSON 主体的编码不同或相似——但我真的不确定如何继续。我希望有人能提供帮助!
最佳答案
您的 putstr
值似乎是一个 JSON 值。
AddJsonBody
会将此 JSON 值转换为另一个 JSON 值。
您应该使用原始对象而不是 putstr
。
关于c# - Put call 在 PostMan 中有效,但在 RestSharp 中无效 : Getting a bad request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50438917/