I know that similar questions have been already posted in the past but I read a lot on the subject and still couldn't find an answer to my problem.
I have a GET request that works fine on Postman:
I translated it with the Code tool in C# - RestSharp and tested it with my plugin but somehow I always get the same error:
My C# - RestSharp code (It's the one I get with the Postman translator):
var client = new RestClient("https://..........."); //my url
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Cookie", ".........."); //my cookie
IRestResponse response = client.Execute(request);
I tried adding all the headers visible in the Headers section:
request.AddHeader("User-Agent", "PostmanRuntime/7.29.0");
request.AddHeader("Accept", "*/*");
request.AddHeader("Accept-Encoding", "gzip, deflate, br");
request.AddHeader("Connection", "keep-alive");
without result. The Authorization type is No Auth. Any idea?
Maybe it has something to do with the fact that the response body in Postman is a plain text? The other requests I send that have a JSON response works perfectly fine.
Thank you for your help!
Use request.AddCookie(cookiename, cookievalue);
Related
My goal is to download a file with a RestSharp GET by passing along a Json body and a security token in the header. For now, I'm just trying to get a response.
I got this code from Postman, where the GET is working (I am prompted where to save the file).
var client = new RestClient(url);
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", token);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", json, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string responseMsg = "";
if (response.Content == "")
responseMsg = response.ErrorMessage
else
responseMsg = response.Content
return responseMsg;
The only response that I'm getting is the ErrorException "HTTP verb GET does not support body".
So I believe there is an issue with my logic, not my Json or token. Does anything in my code look incorrect?
Application framework: .Net 4.8
RestSharp version: 106.15.0
Thanks.
Edit:
As mentioned below, a GET request with a body parameter is invalid. After removing the the json body, the request is now working.
var client = new RestClient(url);
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", token);
IRestResponse response = client.Execute(request);
In the case of my application, the unique identifier was appended to the endpoint, so only the security token was needed as a header.
GET requests cannot have bodies, or rather, they can, but that behaviour is not defined and so some implementations will reject the request entirely. It appears that RestSharp also intentionally does not support GET requests with bodies for the same reason. According to that thread, .NET itself may support them, so you might wish to try that if it's absolutely necessary.
Whether or not you should do that might come under the umbrella of personal opinion, however with that in mind I would caution against doing things outside of the specification, because any updates to libraries or frameworks have no guarantee of consistency of behaviour when they're being used out of spec. If this is an API you have control over, you should consider bringing it in-spec. If it's not, you should consider contacting the developer to have them look into it.
I have this code in C# Windows forms app
string data = "";
var client = new RestClient("http://server:port/ords/xx_portal_dev/oauth/token");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("Cookie", "BCSI-CS-8eb7fa809579930b=1");
request.AddHeader("Content-Length", "29");
request.AddHeader("Accept-Encoding", "gzip, deflate");
request.AddHeader("Host", "server:port");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "*/*");
request.AddHeader("Authorization", "Basic 3244234wfsdf2342234...");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("undefined", "grant_type=client_credentials", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
data = response.Content.ToString();
it work fine in WCF Service Method, but i´m working with windows form application and i get error when run this code on a buttom, I don´t know what is happening...
I always get error Unauthorized "Access to this resource is protected", but in my WCF service it´s working fine!!!
Taken from here. https://stackoverflow.com/a/14628308/1390548
have you tried this to validate the encoding is correct?
request.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
$"{yourusername}:{yourpwd}")));
The above code snippets are working on the client-sides. It is a process of calling the service. But you say that it works well in your WCF service. is it a client application? it should be a server application which creates the WCF service.
Besides, an Unauthorized request usually due to the wrong credential, please check if the below header is valid.
request.AddHeader("Authorization", "Basic 3244234wfsdf2342234...");
Finally, How the client calls depend on the configuration of the service. Please post the complete the server configuration, I will try to write you an example of how to call.
Feel free to let me know if the problem still exists.
I'm trying to access a website that requires login via a form.
I used the Postman HTTP client.
I tried to do the normally http post request but didn't seem to work, I get a successful status code (200 OK) but it doesn't log in, eventually did work with a GET request with BODY parameters (I hadn't seen GET request with body parameters).
Well, I tried to simulate this request in C# code with no luck, I even tried the generated code that Postman offers with no luck again.
Down below is the Postman request and the C# code snippet based on auto-generated Postman code. Does anyone know if is there to make this request with any library or if there is something that I miss?
Thank you in advance.
var client = new RestClient("https://thessalia-3.teilar.gr/login.asp");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Referer", "https://thessalia-3.teilar.gr/login.asp");
var parameters = new Dictionary<string, string>();
parameters["userName"] = JsonConvert.SerializeObject("myusername");
parameters["pwd"] = JsonConvert.SerializeObject("mypass");
parameters["loginTrue"] = JsonConvert.SerializeObject("extravalue");
var content = new FormUrlEncodedContent(parameters);
request.AddParameter("application/x-www-form-urlencoded", content);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Console.WriteLine(response.StatusCode);
Postman Request Photo
Edit:
Postman Request Body Parameters
I've also tried to run this but also not logged in.
Auto-generated code form Postman
If the request was successful (200) and you got the HTML page for "Invalid Credentials", then your code that's making the request should be fine and the issue is with the credentials. Like I said in my first comment, don't serialize the parameters to JSON, URL-encode them instead:
parameters["userName"] = HttpUtility.UrlEncode("myusername");
parameters["pwd"] = HttpUtility.UrlEncode("mypass");
parameters["loginTrue"] = HttpUtility.UrlEncode("extravalue");
This is the standard way and it works with writing the parameters directly to the request stream, or with a utility class like StringContent. However, since you're using the utility class FormUrlEncodedContent, it URL-encode them for you, so you don't have to. In that case, simply assign them directly as string:
parameters["userName"] = "myusername";
parameters["pwd"] = "mypass";
parameters["loginTrue"] = "extravalue";
I've created my app in Yelp, got my api key, and things work fine from Postman when executing a business search.
However, when testing from c#, I receive a 401 unauthorized error with a TOKEN_MISSING error that says ""{\"error\": {\"code\": \"TOKEN_MISSING\", \"description\": \"An access token must be supplied in order to use this endpoint.\"}}"".
I'm supplying my api key correctly though, and the Yelp documentation says that's all I need, so I'm not sure what the problem is. Here are 2 separate c# code samples that do NOT work (I've replaced my actual api key with for security concerns):
Example using WebRequest:
var webRequest = WebRequest.Create("http://api.yelp.com/v3/businesses/search?term=Clayton+Bicycle+Center&location=5411+Clayton+Rd%2c+Clayton%2c+CA+94517%2c+US");
webRequest.Method = "GET";
webRequest.Headers.Add("Cache-Control", "no-cache");
webRequest.Headers.Add("Authorization", "Bearer <my_api_key>");
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
var stream = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
var content = stream.ReadToEnd();
Console.Write(content);
Example using RestSharp:
var client = new RestClient("http://api.yelp.com/v3/businesses/search?term=Clayton+Bicycle+Center&location=5411+Clayton+Rd%2c+Clayton%2c+CA+94517%2c+US");
var request = new RestRequest(Method.GET);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Authorization", "Bearer <my_api_key>");
var response = client.Execute(request);
Console.Write(response.Content);
I've examined the requests in Fiddler, and both are sending the same headers as the working Postman search, but both return 401 unauthorized error while Postman returns the search results. Any ideas?
Edit:
Well this is embarrassing, apparently my issue was I was attempting to access the Yelp API via http instead of https. Once I changed to https, everything worked as expected.
Changed endpoint to use https instead of http, works now.
I am trying to use a very simple RestSharp client like this
RestClient client = new RestClient(#"http://hostname:28000");
RestRequest request = new RestRequest(Method.GET);
request.AddParameter("action", "getstatus");
var res = client.Execute(request);
However, this always fails with
System.IO.InvalidDataException: Block length does not match with its complement.
So the response does not contain any data.
What I noticed when I execute the request in a webrowser with a network tracer, it shows the Transfer-encoding as "chunked". This somehow seems to hinder RestSharp.
Running the same request with standard HttpWebRequest works fine.
Any ideas how to solve this with RestSharp?
//edit: The response should be a valid XML.