如何在HPCC ECL中使用SendGrid API发送POST请求?

huangapple go评论97阅读模式
英文:

How to send POST request using sendgrip api in HPCC ECL?

问题

能否有人友好地协助我在HPCC ECL中使用SendGrid API进行POST请求调用?
我已经勤奋地参考了论坛中提供的文档和示例,但我没有找到有关使用SendGrid API进行POST请求的具体信息或方法。

英文:

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.

答案1

得分: 2

以下是翻译好的部分:

你的示例代码中有一些问题,导致JSON请求的格式不符合SendGrid API的要求。

在RECORD布局的长xpath对于读取值很好,但不适用于写入嵌套数据结构。更好的做法是显式构建子数据结构。

另外,一些请求项是JSON对象数组。任何JSON对象数组都应该在输出的RECORD布局中表示为子数据集。

ECL默认将子数据集写出为Dataset/Row布局。您可以在子数据集的xpath中更改此设置。例如{XPATH('content/')};可以移除默认布局中多余的"Row"标签,从而得到API期望的"content": []格式。

对于复杂的输出结构,尝试将其全部构建为RECORD布局中的默认值也会变得复杂。更好的做法是首先将请求构建为内联数据集,然后将其传递给HTTPCALL。

最后,API密钥需要在授权标头中格式化为承载令牌。

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

希望对你有所帮助,Tony

英文:

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(&#39;email&#39;)};
  STRING name {XPATH(&#39;name&#39;)}; 
END;

toRec := RECORD
  STRING email {XPATH(&#39;email&#39;)};
  STRING name {XPATH(&#39;name&#39;)}; 
END;

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

contentRec := RECORD
  STRING type {XPATH(&#39;type&#39;)}; 
  STRING value {XPATH(&#39;value&#39;)}; 
END;

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


responseRec := RECORD
  STRING msg; 
END; 

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

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

SENDGRID_API_KEY :=&#39;&lt;API-Key&gt;&#39;;
SENDGRID_AUTH_HEADER := &#39;Bearer &#39; + SENDGRID_API_KEY;

responseRec doResponse := TRANSFORM
  SELF.msg := &#39;ERROR: &#39; + failcode + &#39; &#39; + failmessage;
END;

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

HTH,

Tony

答案2

得分: 0

HTTPCALL函数是你所需的,使用 "POST" 作为 httpmethod 参数。虽然这还没有文档化,但你可以在这个之前的Stack Overflow问题的答案中看到一个示例:如何在HPCC ECL中发送POST请求?

祝好运,

Richard

英文:

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

huangapple
  • 本文由 发表于 2023年6月15日 18:28:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定