twiliowebhookssendgridsendgrid-api-v3sendgrid-api-v2

How to use LIKE operator for msg_id or from_email or to_email in query for sendgrid email activity?


 var queryParams = "msg_id LIKE'pBRuJA0OSqyRAHaT2sW8hg'";
var client = new RestClient("https://api.sendgrid.com/v3/messages?query=" + queryParams + "&limit=1");
var request = new RestRequest(Method.GET);
client.Timeout = -1;
request.AddHeader("x-query-id", "{{x-query-id}}");
request.AddHeader("x-cursor", "{{x-cursor}}");
request.AddHeader("authorization", "bearer " + ApiKey);
IRestResponse response = client.Execute(request);
var sendGridEmailDetails = JsonConvert.DeserializeObject<SendGridResponse>(response.Content);

Here I want to find out msg_id start with this value 'pBRuJA0OSqyRAHaT2sW8hg' and I code for this as above. I used LIKE operator here but it gives me empty response. How to use LIKE operator if we want to find out startwith or endwith values? Does anyone know how this could be done? Thanks for your help & time.


Solution

  • This might be an empty result because you are not url encoding the request. It looks like you are using RestSharp to make requests to the API here, so what you can try instead is:

    var params = new {
      query = "msg_id LIKE 'pBRuJA0OSqyRAHaT2sW8hg'",
      limit = 1
    }
    var client = new RestClient("https://api.sendgrid.com/v3/messages");
    var request = new RestRequest(Method.GET);
    request.addObject(params);
    
    client.Timeout = -1;
    request.AddHeader("x-query-id", "{{x-query-id}}");
    request.AddHeader("x-cursor", "{{x-cursor}}");
    request.AddHeader("authorization", "bearer " + ApiKey);
    IRestResponse response = client.Execute(request);
    var sendGridEmailDetails = JsonConvert.DeserializeObject<SendGridResponse>(response.Content);
    

    This way you create an object of the parameters you want to pass and let the request object handle encoding them.