3

I am trying to search all favourite Filters (JIRA) from the actual User using c# HttpWebRequest and Rest-Api. I am still able to read Issues but the filters aren't working.

Reading Issues works as follows:

For example I have this URL to get all Issues from project IT:

http://jira-test.myServer.de/rest/api/2/search?jql=project=%22IT%22

I am using DataContractJsonSerializer to swap the JSON Response to C#-Objects.

From this class I am getting an object after Serialization:

[DataContract]
internal class Kopf
{
    [DataMember]
    public string startAt = string.Empty;

    [DataMember]
    public string maxResults = string.Empty;

    [DataMember]
    public string total = string.Empty;

    [DataMember]
    public Issues[] issues = null;
}

The first lines of JSON are looking like this:

{"expand":"schema,names","startAt":0,"maxResults":50,"total":23044,"issues":[{"expand":"operations,editmeta,changelog,transitions,renderedFields","id":"40000","self":"http://jira-test.myServer.de/rest/api/2/issue/40000","key":"IT-23237","fields":

So I can't understand why the following isn't working for me: This URL give me the right JSON in Browser:

http://jira-test.myServer.de/rest/api/2/filter/favourite

First lines of JSON:

[{"self":"http://jira-test.myServer.de/rest/api/2/filter/10119","id":"10119","name":"Aktiv","description":"Alle Aufgaben die gerade aktiv von mir bearbeitet werden.","owner":{"self":"http://jira-test.myServer.de/rest/api/2/user?username=sb9923","key":"sb9923","name":"sb9923","avatarUrls":{"16x16":"http://jira-test.myServer.de/secure/useravatar?

And here is my Object which I want to serialize:

[DataContract]
internal class FilterData
{
    [DataMember]
    public FilterKopf[] filter = null;
}

[DataContract]
internal class FilterKopf
{
    [DataMember]
    public string id = string.Empty;

    [DataMember]
    public string name = string.Empty;

    [DataMember]
    public string description = string.Empty;

    [DataMember]
    public string jql = string.Empty;
}

I don't get any Exception or something but the FilterKopf Array in the FilterData-Object is always null.

I hope someone can help me with this. I think my C#-Class is the problem because the JSON seems fine and my browser gives the right output.

luviktor
  • 2,240
  • 2
  • 22
  • 23
Sebi
  • 3,879
  • 2
  • 35
  • 62
  • I think the problem is that the issues array started named issues:[{ but the filter response started within a array which is not declared before [{self. I dont know how i can use this in C#. – Sebi Sep 04 '15 at 11:18

1 Answers1

2

If I understand right your problem is that the result contains an array of "Filter" objects but you want to deserialize it as a simple object containing the array. So all you need is to deserialize the stream as FilterKopf[] instead of FilterData.

I created a simple request based on this answer (I modified it slightly, e.g. not POST but GET)

public class JiraTest
{
    internal IEnumerable<FilterKopf> GetFavouriteFilters()
    {
        string url = "http://jira-test.myserver.de/rest/api/2/filter/favourite";

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "GET";
        httpWebRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("YOUR_USERNAME:YOUR_PASSWORD"));

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(FilterKopf[]));
        var filterKoepfe = (FilterKopf[])serializer.ReadObject(httpResponse.GetResponseStream());

        return filterKoepfe;
    }
}

[DataContract]
internal class FilterKopf
{
    [DataMember]
    public string id = string.Empty;

    [DataMember]
    public string name = string.Empty;

    [DataMember]
    public string description = string.Empty;

    [DataMember]
    public string jql = string.Empty;
}

With my own account and with my access to our Jira server the results really reflected my favourite filters.

Update

As a second chance, try to use Json.NET instead of DataContractJsonSerializer. Add to the project through NuGet, and replace the two rows of deserialization to these:

FilterKopf[] filterKoepfe = null;
using (Stream stream = httpResponse.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
    string jsonResponse = reader.ReadToEnd();
    filterKoepfe = Newtonsoft.Json.JsonConvert.DeserializeObject<FilterKopf[]>(jsonResponse);
}

Let's take a look what this does.

Community
  • 1
  • 1
luviktor
  • 2,240
  • 2
  • 22
  • 23
  • Hey thank you for your response. I copy your example and it seems absolutly logic. But it doesnt work for me. The array is always empty (not null). If i use the url in Browser i get some filter and all seems to be ok. I have no idea :( – Sebi Sep 08 '15 at 06:36
  • Did you try to use this code as is in a separate console app? Doesn't work either? – luviktor Sep 08 '15 at 06:49
  • Yes i tried in new Console-App now and it didnt work either. Iam just getting empty array. In Browser there are many hits for this url. Reading issues is no problem. This is strange. – Sebi Sep 08 '15 at 07:01
  • One more thought: I got back an empty not null array if I used http protocol to query. Our Jira server, however, uses https protocol. It I switched to https, the query gave back the real result. But I think it's not your problem, because the project query works with http. – luviktor Sep 08 '15 at 08:11
  • I added an another possible option – luviktor Sep 08 '15 at 08:20
  • Thanks for your help. But with https the WebResponse throw error: Cant't connect to Remoteserver. – Sebi Sep 08 '15 at 09:15
  • And what about Json.NET? – luviktor Sep 08 '15 at 10:27
  • I found my problem and i feel dump :-D. Your first solution is absoluty right. I used our admin-user to login. This user don't have any filter. If i use my brother my logon is used for sure. Thank you for your help. – Sebi Sep 08 '15 at 11:33