Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMime
    String sEncodedBody
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  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.

    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End

    Get ComSetBodyFromPlainText Of hoMime "name=John Doe & city=New York" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  URL-encode the body: spaces become "+", reserved characters become %-escapes.
    Send ComUrlEncodeBody To hoMime "utf-8"

    Get ComGetBodyDecoded Of hoMime To sEncodedBody
    Get ComLastMethodSuccess Of hoMime To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sEncodedBody


End_Procedure