jahnavi guggilla
jahnavi guggilla

Reputation: 1

Integration of Sendgrd API in progress 4gl for email functionality

I'm trying to integrate the SendGrid API for email functionality in Progress 4Gl, and I written piece code, but it's not working. Can anyone help me with fixing these or any suggestions?

DEFINE VARIABLE hHttpRequest AS HANDLE NO-UNDO.
DEFINE VARIABLE hHttpResponse AS HANDLE NO-UNDO.
DEFINE VARIABLE cUrl AS CHARACTER NO-UNDO.
DEFINE VARIABLE cRequestPayload AS LONGCHAR NO-UNDO.
DEFINE VARIABLE cResponse AS LONGCHAR NO-UNDO.
DEFINE VARIABLE cErrorMsg AS CHARACTER NO-UNDO.

cUrl = "https://api.sendgrid.com/v3/mail/send".
cRequestPayload = '{ "personalizations": [{ "to": [{ "email": "[email protected]" }] }], "from": { "email": "[email protected]" }, "subject": "Hello, World!", "content": [{ "type": "text/plain", "value": "Heya!" }] }'.  

CREATE SERVER hHttpRequest.
hHttpRequest:CONNECT("-H https://api.sendgrid.com -W Basic -U SG.SEND_GRID_API_KEY -P ").

RUN send-request IN hHttpRequest (
    INPUT cUrl,
    INPUT "POST",
    INPUT cRequestPayload,
    OUTPUT hHttpResponse,
    OUTPUT cErrorMsg).

IF VALID-HANDLE(hHttpResponse) THEN DO:
    hHttpResponse:GET-BODY(cResponse).
    MESSAGE "Response: " cResponse VIEW-AS ALERT-BOX INFO BUTTONS OK.
    DELETE OBJECT hHttpResponse.
END.

IF VALID-HANDLE(hHttpRequest) THEN
    DELETE OBJECT hHttpRequest.

Facing the below errors

"email:" was not found. (293) Could not understand line 17. (193) "to:" was not found. (293) Could not understand line 17. (193) "email:" was not found. (293) Could not understand line 17. (193) "type:" was not found. (293) Could not understand line 17. (193) "personalizations:" was not found. (293) Could not understand line 17. (193)

Upvotes: 0

Views: 82

Answers (1)

Mike Fechner
Mike Fechner

Reputation: 7192

The curly brace is used for include files - hence the "not found" errors.

Escape the curly brace with the tilde.

cRequestPayload = '~{ "personalizations": [~{ "to": [~{ "email": "[email protected]" }] }], "from": ~{ "email": "[email protected]" }, "subject": "Hello, World!", "content": [~{ "type": "text/plain", "value": "Heya!" }] }'.  

I may have missed one or two in the line above.

For more complex JSON I'd look into the JsonObject Model to avoid issues like that.

Upvotes: 4

Related Questions