Rest, Logging in and Cookies

Hi All,

hope someone can help! Just written a little interface for clients to post bugs they find directly to youtrack using rest and c#

It works fine apart from persisting the login....

this is the login code:

string url = Base_Url + "user/login?";

NameValueCollection formData = new NameValueCollection();

formData["login"] = "username";

formData["password"] = "password";

WebClient serviceRequest = new WebClient();

try{

     byte[] responseBytes = serviceRequest.UploadValues(url, "POST", formData);

     string response = Encoding.UTF8.GetString(responseBytes);      

}

catch {

}

Which logs the user in and creates a cookie called JSESSIONID - which i assume i can then post off with subsequent requests?

However the path is set on the cookie, "/youtrack" so i can't read it in .net?

whats going on? in the example here: http://confluence.jetbrains.net/display/YTD2/Log+in+to+YouTrack the example response doesn't set a path..?

Cheers in advance for any help...

Rob

5 comments
Comment actions Permalink

Hello Rob

     Please try the following code snippet:

     //Create request

      HttpWebRequest loginRequest = (HttpWebRequest) WebRequest.Create("login uri");

      loginRequest.Method = "POST";

      //Write parameters

      using(Stream s = loginRequest.GetRequestStream())

      {

        byte[] bytes = Encoding.UTF8.GetBytes("login=<user_login>&password=<user_password>");

        s.Write(bytes, 0, bytes.Length);

      }

      HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();

      HttpWebRequest authenticatedRequest = (HttpWebRequest) WebRequest.Create("some useful action uri");

      authenticatedRequest.Headers.Add(HttpRequestHeader.Cookie, loginResponse.Headers[HttpResponseHeader.SetCookie]);

      //Now the request has the right cookie

Andrey Serebryansky

Support Engineer

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"

0
Comment actions Permalink

Thanks for the reply!

I'm still getting problems though, on this line

HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();
The remote server returned an error: (400) Bad Request.

p.s this is the uri i put in, which has worked previously!

http://servername:8080/youtrack/rest/user/login?

Cheers

Rob

0
Comment actions Permalink

Rob,

Did you enable REST API on Youtrack Setup screen?

0
Comment actions Permalink

Hi Vadim,

Yes it must be enabled because i can:

Get projects;

Get issues;

Create issues;

Create attachments

etc

The only thing i can't do is persist login (I have the pass the login and password each time currently)

Cheers

Rob

0
Comment actions Permalink

Hello Rob

     Please add

     loginRequest.ContentType = "application/x-www-form-urlencoded";

     after loginRequest.Method = "POST";. Let me know if this helps.

Andrey Serebryansky

Support Engineer

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"

0

Please sign in to leave a comment.