Sample code for 30+ languages & platforms
Unicode C

URL-Encode a MIME Body

See more MIME Examples

Demonstrates the Chilkat Mime.UrlEncodeBody method, which replaces the current body with an application/x-www-form-urlencoded-style representation using the named charset. The only argument is the charset; it is a void method.

Background: This transforms the body into the form-encoding used by HTML form submissions — spaces to +, reserved characters to percent-escapes — interpreting the text through the given charset first so non-ASCII characters encode correctly. It is a niche operation for constructing form-style payloads within a MIME part.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkMimeW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkMimeW mime;
    const wchar_t *encodedBody;

    success = FALSE;

    //  Demonstrates the Mime.UrlEncodeBody method, which replaces the current body with an
    //  application/x-www-form-urlencoded representation using the named charset.  The only argument is
    //  the charset.  It is a void method.

    mime = CkMimeW_Create();

    success = CkMimeW_SetBodyFromPlainText(mime,L"name=John Doe & city=New York");
    if (success == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkMimeW_Dispose(mime);
        return;
    }

    //  URL-encode the body: spaces become "+", reserved characters become %-escapes.
    CkMimeW_UrlEncodeBody(mime,L"utf-8");

    encodedBody = CkMimeW_getBodyDecoded(mime);
    if (CkMimeW_getLastMethodSuccess(mime) == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkMimeW_Dispose(mime);
        return;
    }

    wprintf(L"%s\n",encodedBody);


    CkMimeW_Dispose(mime);

    }