1 2 Previous Next 23 Replies Latest reply on May 20, 2020 8:01 AM by Carl Wilson

    Create JWT with RestSharp

    Girish G
      Share This:


      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

        • 1. Re: Create JWT with RestSharp
          Carl Wilson

          Hi,

          A few questions:

           

          • Are you accessing the correct URL for the Rest API in Remedy, including the correct port?
          • Have you configured the Rest API for your version or are just attempting to use the Mid-Tier link and changing the context?
          • By default, ports 8008/8443 are used for Rest, so have you configured these differently and tested the URL in a browser?

           

          Cheers

          Carl

          1 of 1 people found this helpful
          • 2. Re: Create JWT with RestSharp
            Girish G

            Hi Wilson,

             

            Thanks for the response.

             

            • Are you accessing the correct URL for the Rest API in Remedy, including the correct port?
            • Have you configured the Rest API for your version or are just attempting to use the Mid-Tier link and changing the context?
              • REST API has given by our remedy team. Not sure how exactly they have configured.
            • By default, ports 8008/8443 are used for Rest, so have you configured these differently and tested the URL in a browser?
              • How to do that?

            The same url is working fine when I tested in postman and received the correct response.. Even I have copied the code from postman.

            I could not figure out why its not working in my solution though its working fine in postman. Is there any issue in my code ?

             

            Please find the code snippet which I got from postman.

             

            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.AddHeader("Content-Type", "application/x-www-form-urlencoded");

            request.AddParameter("username", "testuser");

            request.AddParameter("password", "Passw0rd");

            IRestResponse response = client.Execute(request);

            Console.WriteLine(response.Content);

             

            Please let me know if you have any questions.

             

            Thanks in advance!!

            • 3. Re: Create JWT with RestSharp
              Carl Wilson

              Hi,

              first off, my name is Carl as stated in the reply and my name.

               

              Generally a 406 error indicates an issue with the Rest response usually to do with a missing format for the accept of the return value, where the client needs to tell the server what type of value it can accept otherwise it will use the format in "Content-Type".

              The API login call should return the following (string):

               

              ReturnsAn encoded string in the response body referred as TOKEN.

               

              As you are expecting a string try adding the following (or similar "text/plain", "test/json", etc.):

               

              request.AddHeader("Accept", "text/xml")

               

              Cheers

              Carl

              • 4. Re: Create JWT with RestSharp
                Girish G

                Hi Carl,

                 

                Added header in the code request.AddHeader("Accept", "text/xml"); But still got an error.

                 

                please find the response.content as below.

                 

                <html>

                <head>

                <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>

                <title>Error 406 Not Acceptable</title>

                </head>

                <body><h2>HTTP ERROR 406</h2>

                <p>Problem accessing /api/jwt/login. Reason:

                <pre>    Not Acceptable</pre></p>

                </body>

                </html>

                 

                Thanks in advance.

                • 5. Re: Create JWT with RestSharp
                  LJ LongWing

                  Girish,

                  I have looked over your code, and as Carl pointed out, assuming that your port is proper (working in Postman), then I can verify that what you are providing as input should be acceptable to the Remedy Jetty Server....I checked one of my projects and verified that I'm providing the same inputs as you with successful connections.

                   

                  Carl is also correct that a 406 is generally that the server doesn't know what sort of output to provide so throws an error....but the Remedy server doesn't need this and provides a text output even without the need to add the Accept header....so, the only thing that makes any sense to me is that the URL you are using isn't the proper one....but if it's working in Postman with exactly the same url, then I'm not sure what to say.

                  1 of 1 people found this helpful
                  • 6. Re: Create JWT with RestSharp
                    Girish G

                    Thanks LJ for your response.

                     

                    In postman everything work fine, I used the same url , content-type in header , username/password in body and got the response as JW token.

                    Getting error when copied the same code from postman and used in visual studio console application with C#. Did I missed anything in the code or settings which I need to perform.

                     

                    As per my opinion it should as its working fine in postman. Really confused how to troubleshoot, where to start.

                     

                    Any help would be really appreciated.

                     

                    Thanks in advance.

                    • 7. Re: Create JWT with RestSharp
                      Carl Wilson

                      RestSharp & Remedy

                       

                      As mentioned, add the correct Accept Header:

                       

                      request.AddHeader("Accept", "*/*")

                      1 of 1 people found this helpful
                      • 8. Re: Create JWT with RestSharp
                        Girish G

                        Hi Carl,

                         

                        I tried different headers like request.AddHeader("Accept", "text/plain"); , request.AddHeader("Accept", "text/xml"); , request.AddHeader("Accept", "text/html");

                         

                        I got below error when I used header request.AddHeader("Accept", "text/plain");

                         

                        <head>

                        <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>

                        <title>Error 500 </title>

                        </head>

                        <body>

                        <h2>HTTP ERROR: 500</h2>

                        <p>Problem accessing /api/jwt/login. Reason:

                        <pre>    Request failed.</pre></p>

                        <hr />

                        </body>

                         

                         

                        I got below error when I used header request.AddHeader("Accept", "text/xml");  and request.AddHeader("Accept", "text/html");

                        <head>

                        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>

                        <title>Error 406 Not Acceptable</title>

                        </head>

                        <body><h2>HTTP ERROR 406</h2>

                        <p>Problem accessing /api/jwt/login. Reason:

                        <pre>    Not Acceptable</pre></p>

                        </body>

                         

                        Still its not working even I tried different things. I just copied the code from postman.

                        Is there any additional setting which I need perform in visual studio solution?

                         

                        Thanks a lot for your support and time.

                        • 9. Re: Create JWT with RestSharp
                          Carl Wilson

                          Did you try the accept header "*/*" I posted and as confirmed as working in the other discussion?

                          • 10. Re: Create JWT with RestSharp
                            Girish G

                            Hi Carl,

                             

                            Yes, I tried header as request.AddHeader("Accept", "*/*"). Still its not working

                            Please find the response.content as below.

                             

                            <head>

                            <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>

                            <title>Error 500 </title>

                            </head>

                            <body>

                            <h2>HTTP ERROR: 500</h2>

                            <p>Problem accessing /api/jwt/login. Reason:

                            <pre>    Request failed.</pre></p>

                            <hr />

                            </body>

                            • 11. Re: Create JWT with RestSharp
                              Carl Wilson

                              Hi,

                              then I guess I am out of ideas as the issue is not Remedy it is in the .NET class you are using to invoke the Rest call.

                              Did you look at the other thread, it appears they used the same class in .NET and got it working using the "*/*", or maybe use a different Rest method in .NET?

                               

                              Cheers

                              Carl

                              1 of 1 people found this helpful
                              • 12. Re: Create JWT with RestSharp
                                Carl Wilson

                                Hi,

                                Request Failed (500) is different from the original error, so I am guessing you are one step further towards a solution.

                                 

                                Cheers

                                Carl

                                • 13. Re: Create JWT with RestSharp
                                  LJ LongWing

                                  yesterday when I said I had a project that was providing the same inputs....there was one difference....I set a parameter of

                                   

                                  username=user&password=pass

                                   

                                  you are setting two separate parameters....I don't know if that is the difference between a working and non-working call....but it's the only difference I noted...

                                  1 of 1 people found this helpful
                                  • 14. Re: Create JWT with RestSharp
                                    Carl Wilson

                                    Hi,

                                    I think the issue is with the way the values are passed using the class into the call.

                                    The RestSharp appears to have an Authenticator where you can set the parameters for username/password.

                                     

                                    Example:

                                     

                                     

                                    using RestSharp;
                                    using RestSharp.Authenticators;

                                    var client = new RestClient("https://api.twitter.com/1.1");
                                    client.Authenticator = new HttpBasicAuthenticator("username", "password");

                                    var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json);

                                    var response = client.Get(request);

                                     

                                    So modify your call to set the authenticator and then use this in the call.

                                     

                                    Cheers

                                    Carl

                                    1 of 1 people found this helpful
                                    1 2 Previous Next