Search code examples
c#bulksms

bulksms URI for retrieving messages sent


BulkSMS, Retrieve messages from a data range (>= or >) without the imposed limit

The Json is fine both sending and parsing the return data, its the submission URI, I'm struggling with. I'm Getting Data returned in the correct format, just not enough, so its not the process its the URI format.

code at bottom:

After a day of educated guesses about what the correct syntax for this URI should be and reading the api manual which to its credit has great examples for sending SMS messages.

It's main purpose I guess. I'm trying to get a list of messages we have sent for a date range or since a certain date.

string myURI = "https://api.bulksms.com/v1/messages?filter=submission.date%3E%3D2018-01-01T10%3A00%3A00%2B01%3A00";

edit - %3E equals > -------- %3D equals =

so this un-coded means all messages since the start of the year, however the api suggests that the limit of the number of messages is 1000, okay, but they have a param that can be added to override this, ?limit=3000 for example

When I apply this to my URI I get a bad request error (400), does anyone have some examples that may work ?

api doc: http://developer.bulksms.com/json/v1/#tag/Message%2Fpaths%2F~1messages%2Fget

Cheers

    public static string GetListOfSMSMessages(string body)
    {       
     string myURI = "https://api.bulksms.com/v1/messages?filter=submission.date%3E%3D2018-01-01T10%3A00%3A00%2B01%3A00";
     string myUsername = "validusername";
     string myPassword = "validpassword";
     var request = WebRequest.Create(myURI);
     request.Credentials = new NetworkCredential(myUsername, myPassword);
     request.PreAuthenticate = true;
     request.Method = "GET";
     request.ContentType = "application/ascii"; //"application/json";
        try
        {
            // make the call to the API
            var response = request.GetResponse();
            // read the response and add to list
            List<string> outlist = new List<string>();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                while (!reader.EndOfStream)
                {
                    outlist.Add(reader.ReadLine());
                }
            }
        }
        catch (WebException ex)
        {
            // show the general message
            Console.WriteLine("An error occurred:" + ex.Message);
            // print the detail that comes with the error
            var reader = new StreamReader(ex.Response.GetResponseStream());
            Console.WriteLine("Error details:" + reader.ReadToEnd());
            return "Failed";
        }
        return "Successful";

Solution

  • Here is a working example that includes the limit in the query string:

    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    
        class MainClass
        {
    
            public static void Main(string[] args)
            {
                string myURI = "https://api.bulksms.com/v1/messages?filter=submission.date%3E%3D2018-01-01T10%3A00%3A00%2B01%3A00&limit=2";
                string myUsername = "someuser";
                string myPassword = "somepassword";
                var request = WebRequest.Create(myURI);
                request.Credentials = new NetworkCredential(myUsername, myPassword);
                request.PreAuthenticate = true;
                request.Method = "GET";
                request.ContentType = "application/json";
                try
                {
                    // make the call to the API
                    var response = request.GetResponse();
    
                    // read the response and print it to the console
                    var reader = new StreamReader(response.GetResponseStream());
                    Console.WriteLine(reader.ReadToEnd());
    
                }  catch (WebException ex) {
                    // show the general message
                    Console.WriteLine("An error occurred:" + ex.Message);
    
                    // print the detail that come with the HTTP error 
                    var reader = new StreamReader(ex.Response.GetResponseStream());
                    Console.WriteLine("Error details:" + reader.ReadToEnd());
                }
    
        }
    }