Skip to main content
1 of 5
rene
  • 2.8k
  • 2
  • 17
  • 35

To get the questions in a tag you can simply use the /questions endpoint as that does accept a tagged query parameter, from that document page:

To constrain questions returned to those with a set of tags, use the tagged parameter with a semi-colon delimited list of tags. This is an and contraint ...

The API endpoints can relatively simple be used from any language which offer a web of httpclient. First thing to do is the creation of the correct url and then process the returned http response. When using the HtppClient and the stock DataContractJsonSerializer your first iteration of a .Net 4.5 console app (VS2015) might look like this:

main method

static void Main(string[] args)
{
    // simple call with a tagname
    var questions = GetQuestions("haskell");
    // list the results
    Console.WriteLine(String.Format("{0}, \"{1}\", \"{2}\"", "id", "title", "link"));
    foreach (var q in questions.items)
    {
        Console.WriteLine(String.Format("{0}, \"{1}\", \"{2}\"", q.question_id, q.title, q.link));
    }
}

GetQuestions

Builds the url, calls the API and Deserialises the result to .Net classes

private static wrapper<Question> GetQuestions(string tagname)
{
    // make sure to handle the compressed stream that is returned
    using (var handler = new HttpClientHandler
        {
            AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip
        })
    {
        using (var http = new HttpClient(handler))
        {
        // build url obtained from 
        // https://api.stackexchange.com/docs/questions#order=desc&sort=activity&tagged=haskell&filter=default&site=stackoverflow&run=true
            var apiRequest = new UriBuilder();
            apiRequest.Scheme = "https";
            apiRequest.Host = "api.stackexchange.com";
            apiRequest.Path = "2.2/questions";
            apiRequest.Query = String.Format("order=desc&page=1&pagesize=10&sort=activity&tagged={0}&site=stackoverflow", tagname);
            // do an HTTP GET
            var json = http.GetStreamAsync(apiRequest.Uri).Result;
            // Deserialize the resut with a serializer
            // You can also use JSON.NET
            var serializer = new DataContractJsonSerializer(typeof(wrapper<Question>));
            // cast the result
            return (wrapper<Question>)serializer.ReadObject(json);
        }
    }
}

Data Transfer Objects

These are the classes that will hold the result after deserializing the response stream:

/// <summary>
/// https://api.stackexchange.com/docs/wrapper
/// </summary>
/// <typeparam name="T">a type that be in items</typeparam>
[DataContract]
public class wrapper<T>
{
    [DataMember]
    public List<T> items { get; set;}

    [DataMember]
    public string error_message { get; set; }
    [DataMember]
    public int backoff { get; set; }
}

/// <summary>
/// https://api.stackexchange.com/docs/types/question
/// </summary>
[DataContract]
public class Question
{
    [DataMember]public int question_id { get; set; }
    [DataMember]
    public string title { get; set; }
    [DataMember]
    public string link { get; set; }
}
rene
  • 2.8k
  • 2
  • 17
  • 35