Put call 在 PostMan 中有效,但在 RestSharp 中无效:收到错误请求
Put call works in PostMan but not in RestSharp: Getting a bad request
我想用 Put 命令调用端点。
邮递员
我可以举个例子https://example.com/customers/106
。然后我添加 application/json
类型的 body(在 raw
下)。
当我 Put
这个 body 到终点时,我得到一个 200 OK
.
我使用的端点需要两个自定义 headers 和一个 content-type,这是我在 headers 下制作的。所以我加了三个headers:X-AppSecretToken
、X-AgreementGrantToken
和Content-Type
(to application/json
).
在 RestSharp 中
这里我用的是下面的。 putstr
与我在 Postman 中所做的 body 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,但也不走运。不确定是什么问题。
不过,我认为读到这篇文章的人可能会想出这个问题。我自己的假设是我可能以错误的方式添加了 headers,JSON body 的编码不同或相似 - 但我真的不确定如何继续。希望有人能帮忙!
您的 putstr
值似乎是 JSON 值。
AddJsonBody
会将此 JSON 值转换为另一个 JSON 值。
您应该使用原始对象而不是 putstr
。
我想用 Put 命令调用端点。
邮递员
我可以举个例子https://example.com/customers/106
。然后我添加 application/json
类型的 body(在 raw
下)。
当我 Put
这个 body 到终点时,我得到一个 200 OK
.
我使用的端点需要两个自定义 headers 和一个 content-type,这是我在 headers 下制作的。所以我加了三个headers:X-AppSecretToken
、X-AgreementGrantToken
和Content-Type
(to application/json
).
在 RestSharp 中
这里我用的是下面的。 putstr
与我在 Postman 中所做的 body 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,但也不走运。不确定是什么问题。
不过,我认为读到这篇文章的人可能会想出这个问题。我自己的假设是我可能以错误的方式添加了 headers,JSON body 的编码不同或相似 - 但我真的不确定如何继续。希望有人能帮忙!
您的 putstr
值似乎是 JSON 值。
AddJsonBody
会将此 JSON 值转换为另一个 JSON 值。
您应该使用原始对象而不是 putstr
。