Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkMime.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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.i = CkMime::ckCreate()
    If mime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMime::ckSetBodyFromPlainText(mime,"name=John Doe & city=New York")
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

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

    encodedBody.s = CkMime::ckGetBodyDecoded(mime)
    If CkMime::ckLastMethodSuccess(mime) = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    Debug encodedBody


    CkMime::ckDispose(mime)


    ProcedureReturn
EndProcedure