[英]Bad request encountered with RestSharp JSON POST request
I am using RestSharp to make a POST request containing a JSON body. 我正在使用RestSharp发出包含JSON正文的POST请求。 But I get a Bad request error. 但是我收到一个错误的请求错误。
Because I have []
and ""
in my JSON I have decided to use Newtonsoft.Json . 因为我的JSON中有[]
和""
,所以决定使用Newtonsoft.Json。 Before using this I couldn't even see a JSON request being formed. 在使用此功能之前,我什至看不到正在形成JSON请求。
I am willing to try MS httpwebrequest
as an alternative. 我愿意尝试使用MS httpwebrequest
作为替代方法。
restClient = new RestClient();
restRequest = new RestRequest(ApiUrl, Method.POST, DataFormat.Json);
var myObject = "{ \"target\" : \"[5,5]\", \"lastseen\" : \"1555459984\" }";
var json = JsonConvert.SerializeObject(myObject);
restRequest.AddParameter("application/json", ParameterType.RequestBody);
restRequest.AddJsonBody(json);
Please note that I am trying to convert a JSON curl to C#. 请注意,我正在尝试将JSON curl转换为C#。 Please see below: 请看下面:
curl -H 'Content-Type: application/json' -X POST -d '{ "target" : [5, 5], "lastseen" : "1555459984", "previousTargets" : [ [1, 0], [2, 2], [2, 3] ] }' http://santized/santized/santized
You appear to be over serializing the data to be sent. 您似乎过度序列化了要发送的数据。
Consider creating an object and then passing it to AddJsonBody
. 考虑创建一个对象,然后将其传递给AddJsonBody
。
//...
restClient = new RestClient();
restRequest = new RestRequest(ApiUrl, Method.POST, DataFormat.Json);
var myObject = new {
target = new []{ 5, 5 },
lastseen = "1555459984",
previousTargets = new []{
new [] { 1, 0 },
new [] { 2, 2 },
new [] { 2, 3 }
}
};
restRequest.AddJsonBody(myObject); //this will serialize the object and set header
//...
AddJsonBody
sets content type to application/json
and serializes the object to a JSON string. AddJsonBody
将内容类型设置为application/json
并将对象序列化为JSON字符串。
Why not just this? 为什么不只是这个呢?
restClient = new RestClient();
restRequest = new RestRequest(ApiUrl, Method.POST, DataFormat.Json);
var myObject = "{ \"target\" : \"[5,5]\", \"lastseen\" : \"1555459984\" }";
restRequest.AddParameter("application/json", ParameterType.RequestBody);
restRequest.AddJsonBody(json);
Removed the line where you are serializing a json string. 删除了要序列化json字符串的行。
You could also use: 您还可以使用:
public class RootObject
{
public string target { get; set; }
public string lastseen { get; set; }
}
restClient = new RestClient();
restRequest = new RestRequest(ApiUrl, Method.POST, DataFormat.Json);
RootObject myObject = new RootObject();
myObject.target = "[5,5]";
myObject.lastseen = "1555459984";
var json = JsonConvert.SerializeObject(myObject);
restRequest.AddParameter("application/json", ParameterType.RequestBody);
restRequest.AddJsonBody(json);
you could do this example: 您可以执行以下示例:
public static IRestRequest PostInformationAndPassToken(JsonObject tokenString, string path, string whatistobePosted)
{
IRestRequest request = new RestRequest(path, Method.POST);
request.AddHeader("Authorization", $"Bearer {tokenString["Token"]}");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/json");
request.AddParameter(whatistobePosted, ParameterType.RequestBody);
return request;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.