Restsharp. How overcome Error Request Entity Too Large? - c#

I have wcf sharepoint. And I use RestSharp client:
var json = new JsonSerializer().Serialize((objectToUpdate));
var request = new RestRequest(apiEndPoint, Method.POST);
request.Timeout = 180000;
request.AddHeader("X-FORMS_BASED_AUTH_ACCEPTED", "f");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("login", _login);
request.AddHeader("password", _password);
request.AddHeader("Content-Length", objectToUpdate.ToString().Length.ToString());
request.AddParameter("text/json", json, ParameterType.RequestBody);
var response = client.Execute<T>(request);
And I get error Request Entity Too Large.
I try add parameter maxRequestLength="{REQUEST_LENGTH}" in config wcf but not
ineffectually.

Related

Restsharp Request gives "Argument 1: cannot convert from 'RestSharp.Method' to 'string?'" error

I am trying to call POST API request using restSharp 108.0.1v.
The code is as below;
var client = new RestClient("https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles");
var request = new RestRequest(Method.Post);
request.AddHeader("x-api-key", "MYAPIKEY");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n\t\"registrationNumber\":\"AA19AAA\"\n}", ParameterType.RequestBody);
RestResponse response = client.Execute(request);
The snippet Method.Post of var request = new RestRequest(Method.Post);gives the error that I mentioned.
Please help me to solve this issue ?
Looking at the documentation here the first parameter of the RestRequest constructor is the subpath to the resource you want to access. Instead you should do something like the following
var client = new RestClient("https://driver-vehicle-licensing.api.gov.uk");
var request = new RestRequest("vehicle-enquiry/v1/vehicles", Method.Post);
// ... or I believe this should work as well:
var client = new RestClient("https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1");
var request = new RestRequest("vehicles", Method.Post);

Bad Request Patch c#

I would like to update the message of redeem channel point. I do the request in insomnia and it works, but when I try do the same in C#, I receive a message error
'Request failed with status code BadRequest'.
Request on insomnia:
Code of c#:
var client = new RestClient($"https://api.twitch.tv/helix/channel_points/custom_rewards?broadcaster_id={broadcastid}&id=06c5ae77-2890-4a7f-a722-52b96062ef82");
var request = new RestRequest();
request.Method = Method.Patch;
request.AddHeader("Authorization", $"Bearer {Properties.Resources.OAuthToken}");
request.AddHeader("Client-ID", $"{Properties.Resources.userid}");
request.AddHeader("Content-Type", "application/json");
//request.AddBody("is_enabled", "true");
//request.AddBody("prompt", $"test");
request.AddBody("{\"is_enabled\": true,\"prompt\":\"test\"}");
request.RequestFormat = DataFormat.Json;
var response = await client.PatchAsync(request);
Console.WriteLine("");
The request is Patch, can read more about request there. But i think the problem is on request.

Get token for Oauth2 - UrlDecoding of the form parameters from the request message failed. The form parameters needs to be url encoded

I am using C#, RestSharp to get the bearer token for a sap exposed RestAPI. Here is my code snippet
var client = new RestClient("https://url/oauth2/token");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("Authorization", "Basic clientusername:clientpassword");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&username=user&password=pwd", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
But no luck - always get below error
{"fault":{"faultstring":"UrlDecoding of the form parameters from the request message failed. The form parameters needs to be url encoded","detail":{"errorcode":"steps.oauth.v2.InvalidRequest"}}}
I used the psotman with same credentials and it worked fine!!
Any idea?
You might need to use the Authenticator property. Try this
var client = new RestClient($"{_apiBaseUrl}/token")
{
Authenticator = new HttpBasicAuthenticator(_clientId, _clientSecret)
};
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", $"grant_type=password&username={username}&password={password}&scope=trust", ParameterType.RequestBody);
var response = client.Execute(request);

Consuming Drupal RestApi with c#

I am working to consume Drupal Rest Api using c#. I am using drupal 7.5 and utilising it's rest services/api following various resources.
I have been successful with google's postman to post the content but when I try to replicate it with c# code I am prompted with forbidden error saying: Access denied for user anonymous.
I am utilising rest-sharp to consume this API. I have researched quite a lot and haven't found solution yet,as well as haven't noticed anyone doing this work in c#.
Following is the code snippet that I have written based on postman
var client = new RestClient("drupasitename/rest/node");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", authorisation);
request.AddHeader("x-csrf-token", token);
request.AddHeader("cookie", cookie);
request.AddHeader("content-type", "application/json");
request.AddHeader("Accept", "application/json");
request.AddParameter("application/json", "{\r\n \"type\":\"page\",\r\n \"title\":\"Page submitted via JSON REST\",\r\n \"body\":{\r\n \"und\":[\r\n {\r\n \"value\":\"This is the body of the page.\"\r\n }\r\n ]\r\n }\r\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
cookie and token are obtained after successful login attempt using c# code.
It would be great if anyone could provide a guidance to solve this issues.
Regards
All you need is adding UserAgent into http request header
I got the user information include the cookie & token .
private login_user LoginAsync(string username,string password)
{
try
{
RestClient client = new RestClient(base_url);
var request = new RestRequest("login", Method.GET);
request.AddHeader("Content-Type", "Application/JSON");
client.Authenticator = new HttpBasicAuthenticator(username, password);
var restResponse = client.Execute(request);
var content = restResponse.Content;
string context_modifytoken = content.ToString().Replace("X-CSRF-Token", "X_CSRF_Token");
var current_login_user = JsonConvert.DeserializeObject<login_user>(context_modifytoken);
current_login_user.data.session_name = restResponse.Cookies[0].Name;
current_login_user.data.session_id = restResponse.Cookies[0].Value;
return current_login_user;
}
catch (HttpRequestException ex) { throw ex; }
}
And the Post section:
RestClient client = new RestClient(base_url);
var request = new RestRequest("v1.0/barcodes", Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("X-CSRF-Token", current_user.data.X_CSRF_Token);
request.AddHeader("content-type", "application/json");
request.AddHeader("Accept", "application/json");
request.AddHeader("cookie", current_user.data.session_name+"="+ current_user.data.session_id);
request.AddHeader("User-Agent", ver);
var queryresult = client.Execute(request);
has error :queryresult = "StatusCode: Forbidden, Content-Type: application/problem+json; charset=utf-8, Content-Length: -1)"
and Drupal log :
restful 2016-09-21 16:32 You do not have access to create a new Sample Barcode... Anonymous (not verified)
I used following to login to drupal
var client = new RestClient("drupalsitename/user/login");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
request.AddHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0");
request.AddParameter("application/json", "{\"name\":\"username\",\n\"pass\":\"password\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
This will provide you with necessary session and token info and then basically you can use those information to make the post, similar to that I have written in the question

Register Endpoint on Cisco ISE with RestSharp

I'm trying to register(POST) an Endpoint on an Cisco ISE 1.3 via the RestSharp Client in a C# Console Application.
I already got it working with GET requests.
The code i used:
String XML = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?> <ns3:endpoint name='name' id='id' description='Desc' xmlns:ns2='ers.ise.cisco.com' xmlns:ns3='identity.ers.ise.cisco.com'> <groupId>04f3c120-f42f-11e2-bd54-005056bf2f0a</groupId> <mac>00:00:CC:Ac:BB:CC</mac> <profileId>576bf7b0-f42f-11e2-bd54-005056bf2f0a</profileId><staticGroupAssignment>true</staticGroupAssignment> <staticProfileAssignment>true</staticProfileAssignment> </ns3:endpoint> ";
var client = new RestClient();
client.BaseUrl = new Uri("https://" + ip + ":9060/ers/config/endpoint");
client.Authenticator = new HttpBasicAuthenticator(user, pw);
var request = new RestRequest();
request.Method = Method.POST;
request.AddHeader("Accept", "application/vnd.com.cisco.ise.identity.endpoint.1.0+xml");
request.AddHeader("Content-Type", "application/vnd.com.cisco.ise.identity.endpoint.1.0+xml; charset=utf-8");
request.AddBody(XML);
request.RequestFormat = DataFormat.Xml;
request.XmlSerializer.ContentType = "application/vnd.com.cisco.ise.identity.endpoint.1.0+xml; charset=utf-8";
var response = client.Execute(request);
When I submit the Code I receive a "Unsuported Media-Type" error.
The Headers are taken from the SDK from Cisco.
I already realized it with curl, SharePoint and SC Orchestrator.
I think that there is a mistake in the combination of the XML to the request but I can't find it.
Any ideas?
You need to add the content type in the headers, it is the same as the accept one:
request.Method = Method.GET; request.AddHeader("Accept",application/vnd.com.cisco.ise.identity.guestuser.2.0+xml"); request.AddHeader("Content-Type","application/vnd.com.cisco.ise.identity.guestuser.2.0+xml");

Categories