0

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";
user1796185
  • 168
  • 2
  • 13
  • Try first using POSTMan to make sure the url and credentials are all valid. TAke a look at: https://stackoverflow.com/questions/30481939/c-sharp-is-webrequest-create-gives-me-a-url-encoded maybe you should not encode url before passing to Create? – Leszek P May 04 '18 at 12:43
  • Leszek, thank you. Credentials are valid as I said I am getting data back. Indeed i'm not encoding, no need for the URI using GET. – user1796185 May 04 '18 at 13:18
  • Probably you want to add '&limit=3000' not '?limit=3000' ampersand to provide more parameters? – Leszek P May 04 '18 at 14:28

1 Answers1

0

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());
            }

    }
}
Willem
  • 917
  • 7
  • 19
  • Thanks you Willem and Leszek both with correct answer, is this just too easy for BulkSMS to include it in their documentation... A lesson for all us Techies here, attention to detail, get the docs right ! – user1796185 May 08 '18 at 08:47