Sample code for 30+ languages & platforms
DataFlex

PostXml vs. PostUrlEncoded

This example shows the differences in the HTTP requests sent by PostXml vs. PostUrlEncoded. With PostXml, the body of the request is the XML document itself. With PostUrlEncoded, the body of the request is composed of URL encoded params, as shown in the example below.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    String sStrXml
    Variant vResp
    Handle hoResp
    Variant vReq
    Handle hoReq
    Handle hoSbUrl
    String sStrResponse
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Move "<pets><lizard>Pip</lizard><dog>Fido</dog></pets>" To sStrXml

    // We can log the exact HTTP requests/responses to a session log file.
    Set ComSessionLogFilename Of hoHttp To "qa_output/sessionLog.txt"

    // Send a POST w/ content-type of "application/xml"
    Send ComSetRequestHeader To hoHttp "Content-Type" "application/xml"

    // Note: This example is only interested in examining (via the session log)
    // the HTTP requests that are sent.  We're just sending the post to chilkatsoft.com
    // even though it's not an endpoint designed to handle the POST. We don't care about
    // the response, even if it's an error response status code..
    Get ComPostXml Of hoHttp "https://www.chilkatsoft.com/" sStrXml "utf-8" To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Send Destroy of hoResp

    // This is the HTTP request that is sent.
    // You'll notice the body of the request is the XML document itself.

    // 	POST / HTTP/1.1
    // 	Content-Type: application/xml
    // 	Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    // 	Connection: keep-alive
    // 	User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0
    // 	Accept-Language: en-us,en;q=0.5
    // 	Accept-Encoding: gzip
    // 	Host: www.chilkatsoft.com
    // 	Content-Length: 48
    // 
    // 	<pets><lizard>Pip</lizard><dog>Fido</dog></pets>

    // --------
    // Now let's do a PostUrlEncoded...
    // The XML is passed in a query param that is contained in the body of the POST.
    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End
    Send ComAddParam To hoReq "theXml" sStrXml
    Get pvComObject of hoReq to vReq
    Get ComPostUrlEncoded Of hoHttp "https://www.chilkatsoft.com/" vReq To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Send Destroy of hoResp

    // This is the HTTP request that is sent.
    // You'll notice the body of the request is composed of URL encoded query params.

    // 	POST / HTTP/1.1
    // 	Content-Type: application/x-www-form-urlencoded
    // 	Host: www.chilkatsoft.com
    // 	Content-Length: 85
    // 
    // 	theXml=%3Cpets%3E%3Clizard%3EPip%3C%2Flizard%3E%3Cdog%3EFido%3C%2Fdog%3E%3C%2Fpets%3E
    // 

    // --------
    // Let's try one more thing...
    // If the query params are not too large, they can be passed in the URL (which means the query params are passed
    // in the HTTP start line.  This is what happens when a URL w/ query params are pasted in a browser.
    // The HTTP verb is "GET" instead of "POST".

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbUrl
    If (Not(IsComObjectCreated(hoSbUrl))) Begin
        Send CreateComObject of hoSbUrl
    End
    Get ComAppend Of hoSbUrl "https://www.chilkatsoft.com/?" To iSuccess
    Get ComGetUrlEncodedParams Of hoReq To sTemp1
    Get ComAppend Of hoSbUrl sTemp1 To iSuccess
    Get ComGetAsString Of hoSbUrl To sTemp1
    Showln "URL with query params: " sTemp1
    Get ComGetAsString Of hoSbUrl To sTemp1
    Get ComQuickGetStr Of hoHttp sTemp1 To sStrResponse

    // The URL with query params is:  https://www.chilkatsoft.com/?theXml=%3Cpets%3E%3Clizard%3EPip%3C%2Flizard%3E%3Cdog%3EFido%3C%2Fdog%3E%3C%2Fpets%3E

    // The HTTP request looks like this.  You can see there is no request body, and the query params
    // are in the path part of the start line.

    // 	GET /?theXml=%3Cpets%3E%3Clizard%3EPip%3C%2Flizard%3E%3Cdog%3EFido%3C%2Fdog%3E%3C%2Fpets%3E HTTP/1.1
    // 	Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    // 	Connection: keep-alive
    // 	User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0
    // 	Accept-Language: en-us,en;q=0.5
    // 	Accept-Encoding: gzip
    // 	Host: www.chilkatsoft.com


End_Procedure