I figured it out. I commented this line and it worked. To be honest I have no idea what this line of code does.
request.AddParameter("text/plain", body, ParameterType.RequestBody);
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The class below is from a .net 7.0 MAUI app and it retrieves data from a REST API.
When I run the program from "Windows Machine" it works fine. However, when I run the program from and android simulator or Android phone it doesn’t work.
I call the method “GetPilots()” which then call “GetToken()” to get a token from the RESTApi. This part works fine. I verified that the has been gotten successfully.
The method continues executing and at line “RestResponse response = client.Execute(request);” I get the following the following error "Method Not Allowed" at line"
public class PilotSQLServerRepository
{
public string GetToken() {
var client = new RestClient("https://<domain>/");
var request = new RestRequest("Login", Method.Post);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", ParameterType.RequestBody);
RestResponse response = client.Execute(request);
var result = (JObject)JsonConvert.DeserializeObject(response.Content);
var token = result["result"].Value<string>();
return token;
}
public void GetPilots() {
string token = GetToken();
var client = new RestClient("https://<domain>/");
try {
var request = new RestRequest("Pilot", Method.Get);
request.AddHeader("Authorization", "bearer " + token.Trim((char)34));
var body = @"";
request.AddParameter("text/plain", body, ParameterType.RequestBody);
RestResponse response = client.Execute(request); // On and android phone or simulator this line returns
// METHODE NOT ALLOWED. On a Windows Machine the
// the class returns the correct result from the RESTApi
var pilot = JsonConvert.DeserializeObject<List<Pilot>>(response.Content);
// Console.WriteLine("Code = " + response.StatusCode);
Console.ReadKey();
// var result = await Task.FromResult(new List<Pilot>());
}
catch (Exception ex) {
}
}
}
I figured it out. I commented this line and it worked. To be honest I have no idea what this line of code does.
request.AddParameter("text/plain", body, ParameterType.RequestBody);