我想用Put命令调用一个端点。
在邮递员中
我可以举个例子https://example.com/customers/106
。然后,我添加一个类型的主体application/json
(在之下raw
)。
当我Put
将此主体连接到端点时,我得到一个200 OK
。
我使用的端点需要两个自定义标头和一个内容类型,它们是在标头下创建的。所以我添加三个标题:X-AppSecretToken
,X-AgreementGrantToken
和Content-Type
(to application/json
)。
在RestSharp中
在这里,我使用以下内容。这与我在邮递员中所做putstr
的完全一样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内部的调用。我也安装了查尔斯,但也没有运气。不确定是什么问题。
但是,我认为阅读此书的人可能会提出这个问题。我自己的假设是,我可能以错误的方式添加了标头,JSON主体的编码方式不同或相似,但是我不确定如何继续。我希望有人能帮帮忙!
您的putstr
值似乎是JSON值。
AddJsonBody
会将这个JSON值转换成另一个JSON值。
您应该使用原始对象而不是putstr
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句