'v107 Getting Status Code of MethodNotAllowed on a Post Request that Used to Work
I am trying to get an auth token using RestSharp v107 and I cannot figure out how to make it work. I have included the previous method that worked fine until updating to v107. Any ideas?
v107 Code that returns StatusCode = MethodNotAllowed:
private static async System.Threading.Tasks.Task<string> GetAuthTokenAsync()
{
GetCreds();
var client = new RestClient(_authUrl);
var request = new RestRequest();
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "client_credentials");
client.Authenticator = new HttpBasicAuthenticator(_authClientId, _authClientSecret);
var response = await client.ExecutePostAsync(request);
var deserializedResponse = JsonSerializer.Deserialize<Dictionary<string, string>>(response.Content);
string authToken = deserializedResponse["access_token"];
return authToken;
}
Old Code that worked fine before v107:
private static string GetAuthToken()
{
GetCreds();
var client = new RestClient(_authUrl);
var request = new RestRequest() { Method = Method.POST };
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "client_credentials");
client.Authenticator = new HttpBasicAuthenticator(_authClientId, _authClientSecret);
var response = client.Execute(request);
var deserializer = new JsonDeserializer();
var deserializedResponse = deserializer.Deserialize<Dictionary<string, string>>(response);
string authToken = deserializedResponse["access_token"];
return authToken;
}
Solution 1:[1]
See what happens if you define the method at instantiation:
var request = new RestRequest(_authUrl, Method.Post)
.AddHeader("Content-Type", "application/x-www-form-urlencoded")
.AddParameter("grant_type", "client_credentials");
(also you can add parameters in the same statement)
Let me know if it has been resolved, because i'm struggling with v107 too :D
Solution 2:[2]
I would suggest opening an issue in the RestSharp repository, so I can help you.
There are a couple of changes in your code that I would recommend.
- Do not instantiate RestSharp for each call. You have a private property
_authUrl, so instead you should instantiateRestClientin the constructor and keep it in a private field like_client - Don't add content type headers manually. RestSharp adds all the necessary content headers based on the request type and parameters.
You can trace the request using, for example, HttpTracer, as described in the docs. You can then easily see if the request is somehow broken.
The code could look like this:
private async Task<string> GetAuthTokenAsync()
{
GetCreds();
var request = new RestRequest().AddParameter("grant_type", "client_credentials");
var response = await client.PostAsync<Dictionary<string, string>>(request);
return response["access_token"];
}
UPDATE The issue was caused by some wrong values in the Accept header set by RestSharp, which has been fixed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bálint Molnár |
| Solution 2 |
