Parameter count mismatch error in RestSharp? - c#

public IRestResult Send(MessageEnvelope envelope)
{
var request = new RestRequest(Method.POST);
request.AddBody(envelope);
request.RequestFormat = DataFormat.Json;
var responce = _restClient.Execute(request);
return new RestResult
{
Success = responce.StatusCode == HttpStatusCode.OK,
ErrorMessage = responce.Content
};
}
When I pass the envelpoe value I had a runtime error call
Parameter count mismatch
in the line containing request.AddBody(envelope);.
(when I add values to AddBody method).
How can I fix this?

Our solution was to replace the default serializer with JSON .NET
I used the instructions here:
https://github.com/restsharp/RestSharp/blob/master/readme.txt
However, you now have to set the serializer on the request and not at the client.
// Use JSON .NET serializer
request.JsonSerializer = new JsonSerializer();

Related

C# Send data with method POST to API

Dont know what i doing wrong, need to fill database with datas from my VSTO Outlook Addin.
jObjectbody.Add( new { mail_from = FromEmailAddress }); mail_from is name of column in database, FromEmailAddress is value from my Outlook Addin
How to send to API https://my.address.com/insertData correctly ?
RestClient restClient = new RestClient("https://my.address.com/");
JObject jObjectbody = new JObject();
jObjectbody.Add( new { mail_from = FromEmailAddress });
RestRequest restRequest = new RestRequest("insertData", Method.POST);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddParameter("text/html", jObjectbody, ParameterType.RequestBody);
IRestResponse restResponse = restClient.Execute(restRequest);
error : Could not determine JSON object type for type <>f__AnonymousType0`1[System.String].
If i try this in Postman (POST->Body->raw->JSON) data are strored in database, except dont use value just data.
{
"mail_from":"email#email.com"
}
thanks for any clue achieve success
You can use restRequest.AddJsonBody(jObjectbody); instead of AddParameter (which I believe adds a query string).
See the RestSharp AddJsonBody docs. They also mention to not use some sort of JObject as it wont work, so you will probably need to update your type as well.
The below might work for you:
RestClient restClient = new RestClient("https://my.address.com/");
var body = new { mail_from = "email#me.com" };
RestRequest restRequest = new RestRequest("insertData", Method.POST);
restRequest.AddJsonBody(body);
IRestResponse restResponse = restClient.Execute(restRequest);
// extra points for calling async overload instead
//var asyncResponse = await restClient.ExecuteTaskAsync(restRequest);

Move Json Object From One Site To Another

I have two project in asp.net webforms with different url. I need to move c# object from one to the other. I tried to serialize it to JSON with JavaScriptSerializer and move it as parameter in the url, but I don't want the client would see the json
A a = new A()
{
val = 1,
val1 = "very long string"
};
var jsonSerialiser = new JavaScriptSerializer();
string data = jsonSerialiser.Serialize(a);
Response.Redirect(service.RedirectToCheckout("http://localhost:44316/PageOnOtherSite.aspx?data=" + data));
Any ideas?
simple post request using HttpClient
using(var client = new HttpClient())
{
response = await httpClient.PostAsync(uri, new StringContent(data));
{

RestSharp - Serializing an # symbol

Using RestSharp 105.2.3
The API I am talking to requires use to send in a json body, but with a #c symbol as part of the field name. This is illegal in C# of course so I can't just use a dynamic object like below.
Is there a way to get the "#c" in the field name?
var client = new RestClient("https://aaa.bbb.com");
var request = new RestRequest(Method.POST);
request.AddJsonBody(new
{
#c=".Something",
username="johnsmith"
});
You could just use a string like this:
var client = new RestClient("https://aaa.bbb.com");
var request = new RestRequest(Method.POST);
string json = "{\"#c\":\".Something\", \"username\":\"johnsmith}";
request.AddJsonBody(json);

Moving from standard .NET rest to RestSharp

For the most part, I have managed quite quickly to move my code from standard .NET code to using RestSharp. This has been simple enough for GET processes, but I'm stumped for POST processes
Consider the following
var request = System.Net.WebRequest.Create("https://mytestserver.com/api/usr") as System.Net.HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json;version=1";
request.Headers.Add("Content-Type", "application/json;version=1");
request.Headers.Add("Accepts", "application/json;version=1");
request.Headers.Add("Authorize", "key {key}");
using (var writer = new System.IO.StreamWriter(request.GetRequestStream())) {
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes("{\n \"firstName\": \"Dan\",\n \"lastName\": \"Eccles\",\n \"preferredNumber\": 1,\n \"email\" : \"testuser#example.com\",\n \"password\": \"you cant get the wood\"\n}");
request.ContentLength = byteArray.Length;
writer.Write(byteArray);
writer.Close();
}
string responseContent;
using (var response = request.GetResponse() as System.Net.HttpWebResponse) {
using (var reader = new System.IO.StreamReader(response.GetResponseStream())) {
responseContent = reader.ReadToEnd();
}
This is fairly straight forward to move across, except for the serialisation code. Is there a particular way this has to be done for RestSharp? I've tried creating an object and using
var json = JsonConvert.SerializeObject(user);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddBody(json);
but the server still comes back with an error.
I'm also currently using JSON.NET for deserialization to an error object when the user passes in bad data. Is there a way I can deserialize to error object based on a single string using RestSharp?
You're close, but you don't need to worry about serialization with RestSharp.
var request = new RestRequest(...);
request.RequestFormat = DataFormat.Json;
request.AddBody(user); // user is of type User (NOT string)
By telling it that the format is JSON, then passing your already-serialized-as-JSON string, RestSharp is actually encoding it again as a string.
So you pass the string: {"firstName":"foo"} and it actually gets sent to the server as a JSON string object: "{\"firstName\":\"foo\"}" (note how your JSON is escaped as a string literal), which is why it's failing.
Note you can also use an anonymous object for the request:
var request = new RestRequest(...);
request.RequestFormat = DataFormat.Json;
request.AddBody(new{
firstName = "Dan",
lastName = "Eccles",
preferredNumber = 1,
// etc..
});
You use the same typed objects with the response (eg, RestSharp deserializes for you):
var response = client.Execute<UserResponse>(request);
// if successful, response.Data is of type UserResponse

How to parse JSON using RestSharp?

var client = new RestClient("http://10.0.2.2:50670/api");
var request = new RestRequest("Inventory", Method.GET);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
// execute the request to return a list of InventoryItem
RestResponse<JavaList<InventoryItem>> response = (RestResponse<JavaList<InventoryItem>>)client.Execute<JavaList<InventoryItem>>(request);
The content returned is a JSON string, an array of objects. The following is a short excerpt of it:
[{"Id":1,"Upc":"1234567890","Quantity":100,"Created":"2012-01-01T00:00:00","Category":"Tequila","TransactionType":"Audit","MetaData":"PATRON 750ML"},{"Id":2,"Upc":"2345678901","Quantity":110,"Created":"2012-01-01T00:00:00","Category":"Whiskey","TransactionType":"Audit","MetaData":"JACK DANIELS 750ML"},{"Id":3,"Upc":"3456789012","Quantity":150,"Created":"2012-01-01T00:00:00","Category":"Vodka","TransactionType":"Audit","MetaData":"ABSOLUT 750ml"}]
The error message:
Operation is not valid due to the current state of the object
What is wrong here? My InventoryItem has the same properties as each object in the JSON string. Am I missing a step?
I suspect that SimpleJson, used in RestSharp can't deserialise to a JavaList.
First I would try deserialising to a:
List<InventoryItem>
Failing that, I recommend ServiceStack.Text - .Net's fastest JSON library; and do:
var response = client.Execute(request);
var thingYouWant = JsonSerializer.DeserializeFromString<List<InventoryItem>>(response.Content);
This is actually what I do myself.
Edit (Thank you to commentators):
In newer versions this would now be:
var deserializer = new JsonDeserializer();
deserializer.Deserialize<List<InventoryItem>>(response);
Failing w/ auto-magic casting, I use this in a pinch:
var rc = new RestClient("https://api-ssl.bitly.com");
var rr = new RestRequest("/v3/link/clicks?access_token={access_token}&link={bitlyUrl}", Method.GET);
rr.AddUrlSegment("bitlyUrl", bitlyUrl);
rr.AddUrlSegment("access_token", BityAccessToken);
var response = rc.Execute(rr);
dynamic json = Newtonsoft.Json.Linq.JObject.Parse(response.Content);
var clicks = Convert.ToInt32(json.data.link_clicks.Value);

Categories