0

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

girish
  • 1
  • 1
  • What is the response type of the service that generates the JWT? Json, xml, plain text etc.. You will need to mention that type in the Accept header of your request. – Anouar May 13 '20 at 12:49
  • @Anouar, Thanks for the response. response type is text. where do I mention the type in header? – girish May 13 '20 at 13:34
  • Try adding `request.AddHeader("Accept", "text/plain");` in your code – Anouar May 13 '20 at 13:53
  • Added in the code request.AddHeader("Accept", "text/plain"); Got another error - Error 500 Request failed. Earlier it was Error 406 Not Acceptable. please find the response.content as below. Error 500

    HTTP ERROR: 500

    Problem accessing /api/jwt/login. Reason:

        Request failed.

    – girish May 13 '20 at 14:31
  • So you are having an error in the server side. Is the service logging any error? Do you have access to the code of that jwt generation service? – Anouar May 13 '20 at 15:42
  • Dont have access to the code. The url has been shared by the Remedy team. My doubt here is if everything works fine when I tested in postman the why its not working in the console app though I have copied the code from postman. Did I missed anything the code or settings which I need to perform? Thanks in advance. – girish May 14 '20 at 04:46
  • Postman doesn't really create a good RestSharp call. Update your answer with the Postman request (headers, body, method, etc) and it will be easier to compose the code for RestSharp. – Alexey Zimarev May 16 '20 at 15:02
  • Working example over here: https://stackoverflow.com/questions/38494279/how-do-i-get-an-oauth-2-0-authentication-token-in-c-sharp – Jeff Blumenthal Mar 20 '23 at 22:39

0 Answers0