1

Could someone kindly assist me in making a POST request call using the SendGrid API in HPCC ECL? I have diligently referred to the documentation and examples provided in the forum, but I have not come across any specific information or methods regarding making a POST request using the SendGrid API.

Richard Taylor
  • 493
  • 2
  • 6

2 Answers2

2

There are a couple of things in your sample code preventing the JSON request from being formatted exactly the way the SendGrid API expects.

Long xpaths within RECORD layouts are great for reading in values, but not for writing out nested data structures. It's better to build out the child data structures explicitly.

Also, several of the request items are JSON object arrays. Any JSON object arrays should be represented in the output RECORD layouts as child datasets.

ECL defaults to writing out child datasets with a Dataset/Row layout. You can change this in the xpath of the child dataset. For example {XPATH('content/')}; removes the extra "Row" tag from the default layout; giving us the "content": [] format the API expects.

For complex output structures it also gets tricky trying to build it all as default values in the RECORD layout. It's better to first build the request as an inline dataset and then pass it to the HTTPCALL.

Finally, the API-Key needs to be formatted as a bearer token within the authorization header.

fromRec := RECORD
  STRING email {XPATH('email')};
  STRING name {XPATH('name')}; 
END;

toRec := RECORD
  STRING email {XPATH('email')};
  STRING name {XPATH('name')}; 
END;

personalizationsRec := RECORD
  DATASET(toRec) receiver {XPATH('to')};
  STRING subject {XPATH('subject')}; 
END;

contentRec := RECORD
  STRING type {XPATH('type')}; 
  STRING value {XPATH('value')}; 
END;

requestRec := RECORD
  fromRec sender {XPATH('from')};
  DATASET(personalizationsRec) personalizations {XPATH('personalizations/')};
  DATASET(contentRec) content {XPATH('content/')};
END;


responseRec := RECORD
  STRING msg; 
END; 

requestDataset := dataset([{{'johndoe@example.com', 'JDoe'}, [{[{'janedoe@example.com','JD'}], 'Hello, World!'}], [{'text/plain', 'Heya!'}]}], requestRec);

requestRec t(requestRec l) := TRANSFORM
 SELF := l;
END;

SENDGRID_API_KEY :='<API-Key>';
SENDGRID_AUTH_HEADER := 'Bearer ' + SENDGRID_API_KEY;

responseRec doResponse := TRANSFORM
  SELF.msg := 'ERROR: ' + failcode + ' ' + failmessage;
END;

OUTPUT(HTTPCALL(requestDataset, 'https://api.sendgrid.com/v3/mail/send', '', requestRec, t(LEFT), DATASET(responseRec), onFail(doResponse), JSON, LOG, HTTPHEADER('Authorization', SENDGRID_AUTH_HEADER)));

HTH,

Tony

0

The HTTPCALL function is what you need, using "POST" as the httpmethod parameter. This is not yet documented, but you can see an example in the answer to this previous Stack Overflow question: How to send POST request in HPCC ECL?

HTH,

Richard

Richard Taylor
  • 493
  • 2
  • 6
  • Thank You for your response, but I have tried the above response and didn't get any response and confused over functionality usage using sendgrid api. – Yashi Mishra Jun 16 '23 at 09:59
  • Example code: sendContent := RECORD string senderName {XPATH('from/name')} := 'Jacob Tyler'; string senderEmail {XPATH('from/email')} := 'Jacob.Tyler@example.io'; string receiveremail {XPATH('personalizations/to/email')} := 'john.dcruz@example.io'; string receivername {XPATH('personalizations/to/name')} := 'John Dcruz'; String subject {XPATH('subject')} := 'hello World'; String type {XPATH('type')} := 'text/plain'; String value {XPATH('value')} := 'Heya!'; END; receiveContent := RECORD String value {XPATH('content/value')}; END; – Yashi Mishra Jun 16 '23 at 10:01
  • receiveRec := RECORD receiveContent content {XPATH('content')}; END; //{"personalizations":[{"to":[{"email":"john.dcruz@example.io","name":"John Dcruz"}], //"subject":"Hello, World!"}],"content": [{"type": "text/plain", "value": "Heya!"}], //"from": {"email":"acob.Tyler@example.io","name":"Jacob tyler"}} OUTPUT(HTTPCALL('https://api.sendgrid.com/v3/mail/send', '', sendContent, DATASET(receiveRec), JSON, LOG, HTTPHEADER('Authorization', 'API-Key'), HTTPHEADER('type', 'text/plain'))); – Yashi Mishra Jun 16 '23 at 10:01