Sample code for 30+ languages & platforms
C Requires Chilkat v11.0.0+

SendGrid: Look up a bounced email address

See more SendGrid Examples

Look up a recipient on the bounce suppression list without changing it. SendGrid returns an array. An empty array means there is no bounce record for this lookup; it does not prove a particular message reached the inbox.

Retrieve a bounce

Chilkat C Downloads

C
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonArray.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkHttpResponse resp;
    HCkJsonArray rows;
    HCkJsonObject row;
    int i;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = FALSE;
    http = CkHttp_Create();
    CkHttp_putRequireSslCertVerify(http,TRUE);
    //  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
    CkHttp_putAuthToken(http,"SENDGRID_API_KEY");
    resp = CkHttpResponse_Create();

    //  SetUrlVar encodes the raw address as a URL component, including the plus sign.
    CkHttp_SetUrlVar(http,"address","avery+orders@example.com");

    success = CkHttp_HttpNoBody(http,"GET","https://api.sendgrid.com/v3/suppression/bounces/{$address}",resp);
    if (success != TRUE) {
        if (CkHttpResponse_getStatusCode(resp) == 0) {
            printf("%s\n",CkHttp_lastErrorText(http));
        }
        else {
            printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
        }

        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        return;
    }

    if (CkHttpResponse_getStatusCode(resp) != 200) {
        printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        return;
    }

    rows = CkJsonArray_Create();
    success = CkJsonArray_Load(rows,CkHttpResponse_bodyStr(resp));
    if (success != TRUE) {
        printf("Unexpected response: expected JSON.\n");
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonArray_Dispose(rows);
        return;
    }

    if (CkJsonArray_getSize(rows) == 0) {
        printf("No bounce record for this address.\n");
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonArray_Dispose(rows);
        return;
    }

    row = CkJsonObject_Create();
    i = 0;
    while ((i < CkJsonArray_getSize(rows))) {
        CkJsonArray_ObjectAt2(rows,i,row);
        printf("%s: %s %s\n",CkJsonObject_stringOf(row,"email"),CkJsonObject_stringOf(row,"status"),CkJsonObject_stringOf(row,"reason"));
        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkHttpResponse_Dispose(resp);
    CkJsonArray_Dispose(rows);
    CkJsonObject_Dispose(row);

    }