Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 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.

    DECLARE @mime int
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @mime, 'SetBodyFromPlainText', @success OUT, 'name=John Doe & city=New York'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    --  URL-encode the body: spaces become "+", reserved characters become %-escapes.
    EXEC sp_OAMethod @mime, 'UrlEncodeBody', NULL, 'utf-8'

    DECLARE @encodedBody nvarchar(4000)
    EXEC sp_OAMethod @mime, 'GetBodyDecoded', @encodedBody OUT
    EXEC sp_OAGetProperty @mime, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    PRINT @encodedBody

    EXEC @hr = sp_OADestroy @mime


END
GO