0

I'm trying to process the resulting URLs from the marketing/contacts/exports/{id} call I used the following code however the data is dataresponse is zipped/encrypted.

SendGridClient client = new SendGridClient(AppConfig.ReadSetting("SENDGRID_API_KEY"));

SingleSendContactList allListsSingleSend = new SingleSendContactList();
var response = await client.RequestAsync(method: SendGridClient.Method.GET, urlPath: "marketing/lists?page_size=500");
allListsSingleSend = JsonConvert.DeserializeObject<SingleSendContactList>(response.Body.ReadAsStringAsync().Result);

SingleSendExportSetup SSExportSetup = new SingleSendExportSetup();
SSExportSetup.file_type = "json";
SSExportSetup.max_file_size = 5000;

// marketing/contacts/exports, POST returns JSON with id as main field
SingleSendExportID ExportContactsSingleSend = new SingleSendExportID();
response = await client.RequestAsync(method: SendGridClient.Method.POST, urlPath: "marketing/contacts/exports", requestBody: JsonConvert.SerializeObject(SSExportSetup));
ExportContactsSingleSend = JsonConvert.DeserializeObject<SingleSendExportID>(response.Body.ReadAsStringAsync().Result);

//Use this id to then call

// marketing/contacts/exports/id, GET returns JSON with urls as a property, use urls[0]
SingleSendExportURL ExportURLSingleSend = new SingleSendExportURL();
response = await client.RequestAsync(method: SendGridClient.Method.GET, urlPath: "marketing/contacts/exports/" + ExportContactsSingleSend.id);
ExportURLSingleSend = JsonConvert.DeserializeObject<SingleSendExportURL>(response.Body.ReadAsStringAsync().Result);

// Call the URL to get the returned JSON
List<SingleSendExportData> ExportDataSingleSend = new List<SingleSendExportData>();
var dataresponse = await client.RequestAsync(method: SendGridClient.Method.GET, urlPath: ExportURLSingleSend.urls[0]);
ExportDataSingleSend = JsonConvert.DeserializeObject<List<SingleSendExportData>>(dataresponse.Body.ReadAsStringAsync().Result);
 

I can take the URL in ExportURLSingleSend.urls[0] and drop it in a browser and it will force the download of the file and the data is fine. How do I get this data to convert to my class List<SingleSendExportData>?

I posted this question to SendGrid support and they sent me here.

Thanks.

UPDATE:

This post solved my issue: https://weblog.west-wind.com/posts/2007/jun/29/httpwebrequest-and-gzip-http-responses

However, now I am realizing that the JSON coming from SendGrid is not properly formatted. There is no comma between rows of data.

UPDATE 2: This post solved my handling of line delimited JSON files; Line delimited json serializing and de-serializing

Now I am able to read these zipped JSON files without saving them and then handle the JSON format to load into my list List ExportDataSingleSend.

Joel
  • 1
  • 1
  • Welcome to StackOverflow. Please try to avoid using blocking async calls `.ReadAsStringAsync().Result`. Please prefer [async-await](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/). – Peter Csala May 25 '21 at 13:07
  • Thanks for the info. – Joel May 25 '21 at 17:28

0 Answers0