YouTrack Rest API Web Exceptions

I am having an issue with using the REST API and I am not sure whether the problem lies in the web requests or my use of the API. I am trying to get a list of all the issues for the given sprint:

Code
var loginRequest = WebRequest.Create( url + "login?" );
loginRequest.Credentials = new NetworkCredentials( user, pw );
var loginResponse = loginRequest.GetResponse();

var authRequest = WebRequest.Create( url + "/rest/project/issues?filter=Sprint:{" + name + "}" );
authRequest.Headers.Add( HttpRequestHeader.Cookie, loginResponse.Headers["Set-Cookie"] );

var authRespose = authRequest.GetResponse();
prettyPrint();

When I run this code, I receive a "401 Unauthorized" error. However, taking that same URI and pasting it into my web browser works fine with the same username and password. However, I can preform other requests, such as "rest/issue/Issue-1" using the same code as above with no errors.

So, do I need elevated user permissions to run a project related method or is there an error in my request?
5 comments
Comment actions Permalink
Hello Sarah,

Could you please provide us with your natural REST request (you can look at it thru FireBug) ?
I assume that this code causes wrong result:
var loginRequest = WebRequest.Create( url + "login?" );
loginRequest.Credentials = new NetworkCredentials( user, pw );
var loginResponse = loginRequest.GetResponse();

Thank you,
Waitig for details from you.
0
Comment actions Permalink
I will get the natural REST request from the person who manages the YouTrack and post it when I get it.

However, that code does work depending on the request I send, as I said above. For example:

This works:
var loginRequest = WebRequest.Create( url + "login?" );
loginRequest.Credentials = new NetworkCredentials( user, pw );
var loginResponse = loginRequest.GetResponse();

var authRequest = WebRequest.Create( url + "/rest/issue/Issue-1" );
authRequest.Headers.Add( HttpRequestHeader.Cookie, loginResponse.Headers["Set-Cookie"] );

var authRespose = authRequest.GetResponse();
prettyPrint();

This does not:
var loginRequest = WebRequest.Create( url + "login?" );
loginRequest.Credentials = new NetworkCredentials( user, pw );
var loginResponse = loginRequest.GetResponse();

var authRequest = WebRequest.Create( url + "/rest/project/issues?filter=Sprint:{" + name + "}" );
authRequest.Headers.Add( HttpRequestHeader.Cookie, loginResponse.Headers["Set-Cookie"] );

var authRespose = authRequest.GetResponse();
prettyPrint();
0
Comment actions Permalink
What are information are you looking for from the FireBug data? I am using Wireshark to look at the network traffic and to my untrained eye I cannot see any reason that the request is failing.
0
Comment actions Permalink
Hi Sarah,

We need to review initial rest request. It'll be great if it's possible to provide us with this details.

Thank you,
Waiting for details from you.
0
Comment actions Permalink
I figured out my own problem. I needed to add a header:

var bytes = Encoding.UTF8.GetBytes( user + ":" + pw );
authRequest.Headers.Add( "Authorization", "Basic " + Convert.ToBase64String( bytes ) );
prettyPrint();
Why I need this header for some requests and not others I do not understand. However, my problem is fixed, so I am fine.
0

Please sign in to leave a comment.