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
Please sign in to leave a comment.
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!"
Thanks for the reply!
I'm still getting problems though, on this line
p.s this is the uri i put in, which has worked previously!
Cheers
Rob
Rob,
Did you enable REST API on Youtrack Setup screen?
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
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!"