When I call serever like this:
var client = new RestClient();
client.Authenticator = new NtlmAuthenticator();
var request = new RestRequest(uri);
request.Method = Method.POST;
request.RequestFormat = DataFormat.Xml;
request.AddParameter("application/atom+xml", doc /*some xml document*/, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Debug.WriteLine(response.Content);
if(response.StatusCode == HttpStatusCode.Created)
{
// always 401
}
I get next server responce:
<?xml version="1.0" encoding="UTF-8"?>
<SmoothStreaming xmlns="http://schemas.microsoft.com/iis/media/2011/03/streaming/management">
<Error>
<ErrorCode>0x80880022</ErrorCode>
<ErrorMessage>All requests to the management APIs must be authenticated. Please install and enable an appropriate IIS authentication module for this website.</ErrorMessage>
</Error>
</SmoothStreaming>
What and where should I change (install)?
Related
I am new to WEB API and I have created a get and a post method the get is working however the the parameter in the post is returning null.
The code for request is
if (ModelState.IsValid)
{
var client = new RestClient(string.Format("{0}/api/order/createorder",Baseurl));
client.Timeout = -1;
var request = new RestRequest(Method.POST);
var test = JsonConvert.SerializeObject(order);
request.AddJsonBody(order);
IRestResponse response = client.Execute(request);
and it points to the following method
[HttpPost]
[Route("api/order/createorder")]
public HttpResponseMessage AddOrder([FromBody]IOrder order)
{
if(order== null)
{
var resp = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("The order is a required parameter"),
ReasonPhrase = "Order not present"
};
throw new HttpResponseException(resp);
}
I have added the <requestLimits maxAllowedContentLength="4294967295" /> to the web config but to no avail.
Can anyone point me to what I am doing wrong with the request?
Cheers
Try this code
var client = new RestClient(string.Format("{0}/api/order/createorder",Baseurl));
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
var body = JsonConvert.SerializeObject(order);
request.AddParameter("application/json; charset=utf-8", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var result = response.Content;
and never use interfaces in action parameters, since the action needs to create an object, but it is impossible from interface
[Route("~/api/order/createorder")]
public HttpResponseMessage AddOrder([FromBody] Order order)
I think if can rewrite you request to this ->
var client = new RestClient("/api/order/createorder",Method.Post, DataFormat.Json);
client.AddJsonBody(order);
IRestResponse response = client.Execute(request);
That might just work, you may need some mapping potentially if the payload you are passing to the controller is a different type.
I need to convert that curl request to c#. Im using RestSharp. curl request:
> curl -X POST -i https://gw.api.alphabank.eu/sandbox/auth/token \
-u "{{client_id}}:{{client_secret}}" \
-d "grant_type=client_credentials&scope=account-info-setup"
I tried the following code but I end up with 'invalid_grant' error as a response.
Any ideas what i'm doing wrong?
My code:
var client = new RestClient(url);
var request = new RestRequest();
request.Method = Method.POST;
client.Authenticator = new HttpBasicAuthenticator(ABclientID, ABclientSecret);
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("scope", "account-info-setup");
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/x-www-form-urlencoded"; };
IRestResponse response = client.Execute(request);
this sample code is generated by Postman and it's working for my api which accepts application/x-www-form-urlencoded, can you add your parameters like this? Or create and make your request works on Postman and generate to C#-RestSharp it's usually works for me with minor changes.
var client = new RestClient("url");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("userId", "1234");
request.AddParameter("count", "5");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Turns out "invalid_grant" meant wrong credentials. I was giving wrong client-secret.
Request was succesfull after correcting the client_secret.
I am trying to create JWT for authenticating REST api. Please find my code below.
private static string getJWT()
{
var client = new RestClient("https://itsmtest-app.XXXXXX.com/api/jwt/login");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("username", "testuser",ParameterType.RequestBody);
request.AddParameter("password", "Passw0rd",ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
return response.Content;
}
I am getting an error. please find response.Content as below. But the rest call working fine in postman even I copied the code postman.
Error 406 Not Acceptable
HTTP ERROR 406
Problem accessing /api/jwt/login.
Reason:Not Acceptable
I am using RestSharp to make a GET api call. The api call is authenticated through HTTP Basic authentication by passing the authorization header.
The server redirects the api call with a status code 307. My client code does handle the redirects but the authorization header is not passed to this redirected api call. This is done for valid reasons as mentioned here. Hence I do get an unauthorized error.
How can I configure the RestClient to restore the authorization header?
var client = new RestClient("https://serverurl.com");
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Basic Z3JvdXAxOlByb2otMzI1");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Tenant-Id", "4892");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
I added a check that resends the api request of receiving a 401 with the below code.
var client = new RestClient("https://serverurl.com");
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Basic Z3JvdXAxOlByb2otMzI1");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Tenant-Id", "4892");
IRestResponse response = client.Execute(request);
//Resend the request if we get 401
int numericStatusCode = (int)response.StatusCode;
if(numericStatusCode == 401) {
var redirectedClient = new RestClient(response.ResponseUri.ToString());
IRestResponse newResponse = redirectedClient.Execute(request);
Console.WriteLine(newResponse.ResponseStatus);
}
I'm trying to write a C# application that will call an api for Pacer.gov. First thing I need to do it get a cookie to pass with my request. I'm using Chrome's Postman app to try to generate a POST request. Can someone explain what I'm doing wrong?
The instructions I got from Pacer.gov to get the authentication token are
For credentials, you can POST username(loginid) and password(passwd) to the authentication service here https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl
This will issue you a PacerSession cookie.
Here are some ways I tried:
(I’m just showing ##### as the login and ***** as the password, but I used the actual login and password when I tried it)
Including username and password in the URL
var client = new RestClient("https://pacer.login.uscourts.gov/cgi-in/check-pacer-passwd.pl?loginid=#####&passwd=*****");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "a5bd0d9c-85f5-4ed6-6ad8-ac06e553f5db");
request.AddHeader("cache-control", "no-cache");
IRestResponse response = client.Execute(request);
o Received HTML page saying “Access to PACER requires a valid account id and password. “
Including username and password in the header
var client = new RestClient("https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "add9dedf-10bf-eab4-428a-134c0e8a1783");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("passwd", "*****");
request.AddHeader("loginid", "#####");
IRestResponse response = client.Execute(request);
o Received HTML page saying “Access to PACER requires a valid account id and password. “
Including username and password in the body as form data
var client = new RestClient("https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "454a8ebd-b2b4-3db2-0c5c-c48e2964c671");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"loginid\"\r\n\r\n#####\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"passwd\"\r\n\r\n*****\r\n-----011000010111000001101001--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
o Received message “Could not get any response”
Typing URL into Chrome with the username and password
https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl?loginid=#####&passwd=*****
o Received error “This site can’t be reached”
UPDATE - 5-31-16
I've made several more attempts, but am still not getting the cookie back. I tried this code:
var client = new RestClient("https://pacer.login.uscourts.gov");
var request = new RestRequest("/cgi-bin/check-pacer-passwd.pl", Method.POST);
request.AddParameter("loginid", "#####");
request.AddParameter("passwd", "*****");
IRestResponse response = client.Execute(request);
When I look at the response, there is one cookie called JESSIONID which appears just to be a generic browser session ID, not the authentication code I'm expecting:
I'm not exactly sure what to expect from the response, but I know that a "NextGenCSO cookie" was mentioned in one of the e-mails from Pacer. If I login through the web browser and look at the cookies, I see a NextGenCSO cookie; I'm guessing I should be seeing something similar in the response from the API call:
UPDATE - 6-3-16
Thanks to help from #jonathon-reinhart, I was able to get the authentication cookie working with this code:
// Authentication
var client = new RestClient("https://pacer.login.uscourts.gov");
client.CookieContainer = new System.Net.CookieContainer();
var request = new RestRequest("/cgi-bin/check-pacer-passwd.pl", Method.POST);
request.AddParameter("loginid", "#####");
request.AddParameter("passwd", "*****");
IRestResponse response = client.Execute(request);
var PacerSession = "";
foreach (RestResponseCookie cookie in response.Cookies)
{
if (cookie.Name == "PacerSession")
{
PacerSession = cookie.Value;
}
}
You are not correctly POSTing as the directions indicate.
Putting this:
https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl?loginid=#####&passwd=*****
into the address bar is not a POST. That's a GET with query string parameters.
See the RestSharp homepage for an example on how to POST:
var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
You want to include loginid and passwd as data ("parameters" as RestSharp calls them), not has headers.
So you should be instead doing something like this:
var client = new RestClient("https://pacer.login.uscourts.gov");
var request = new RestRequest("/cgi-bin/check-pacer-passwd.pl", Method.POST);
request.AddParameter("loginid", "####");
request.AddParameter("passwd", "****");
IRestResponse response = client.Execute(request);