Sample code for 30+ languages & platforms
DataFlex

POST application/x-www-form-urlencoded Two Ways

See more HTTP Examples

This example explains how a POST with params can be formatted in different ways. Params can be encoded in the URL, in which case they appear in the "start line" of the HTTP request, or params can be placed within the body of the request. A properly implemented server SHOULD accept both -- it SHOULD make no difference where the params are located. They are simply the params of a POST. But alas, many servers are not, and we need to cope with the particulars..

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Handle hoHttpB
    Variant vReqB
    Handle hoReqB
    Variant vRespB
    Handle hoRespB
    String sTemp1

    Move False To iSuccess

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

    // We start with this CURL statement:

    // curl -X POST "https://xyz.softeon.com/api/token?grant_type=client_credentials&resource=https://xyz.softeon.com/resources&scope=openid&client_id=AAA&client_secret=BBB" -H "Content-Type: application/x-www-form-urlencoded"

    // Postman would send the following request:

    // POST /api/token?grant_type=client_credentials&resource=https://xyz.softeon.com/resources&scope=openid&client_id=AAA&client_secret=BBB HTTP/1.1
    // Content-Type: application/x-www-form-urlencoded
    // User-Agent: PostmanRuntime/7.26.8
    // Accept: */*
    // Cache-Control: no-cache
    // Postman-Token: 719e6ada-5b0e-4d3f-8166-37433efdcaa1
    // Host: xyz.softeon.com
    // Accept-Encoding: gzip, deflate, br
    // Connection: keep-alive
    // Content-Length: 0

    // We can see that the params are located in the "start line" of the HTTP request, and the request has no body (the Content-Length is 0).

    // You can use Chilkat's online tool to generate the code for the above CURL statement.
    // Generate Source Code from CURL Statement
    // 

    // Here's the generated code:

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

    // We can see the exact HTTP request sent by setting a session log filename:
    Set ComSessionLogFilename Of hoHttp To "qa_output/sessionLog.txt"

    Send ComSetRequestHeader To hoHttp "Content-Type" "application/x-www-form-urlencoded"

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoResp to vResp
    Get ComHttpNoBody Of hoHttp "POST" "https://xyz.softeon.com/api/token?grant_type=client_credentials&resource=https://xyz.softeon.com/resources&scope=openid&client_id=AAA&client_secret=BBB" vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Response body:"
    Get ComBodyStr Of hoResp To sTemp1
    Showln sTemp1

    // Here's what is sent by Chilkat.  It is essentially the same thing:

    // POST /api/token?grant_type=client_credentials&resource=https://xyz.softeon.com/resources&scope=openid&client_id=AAA&client_secret=BBB HTTP/1.1
    // Host: xyz.softeon.com
    // Accept: */*
    // Accept-Encoding: gzip
    // Content-Type: application/x-www-form-urlencoded
    // Content-Length: 0

    // -------------------------------------------------------------------------------------------------------------------------------------------------
    // Now let's look at the alternative way of sending a POST application/x-www-form-urlencoded,
    // where the params are contained within the body of the request.

    // curl -X POST  https://xyz.softeon.com/api/token \
    // -H 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8' \
    // -d 'grant_type=client_credentials' \
    // -d 'resource=https://xyz.softeon.com/resources' \
    // -d 'scope=openid' \
    // -d 'client_id=AAA' \
    // -d 'client_secret=BBB'

    // The Chilkat tool at https://tools.chilkat.io/curlHttp.cshtml
    // generates the following code:
    // (We changed the names of the variables to keep things entirely separate from the above code.)

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

    Set ComSessionLogFilename Of hoHttpB To "qa_output/sessionLogB.txt"

    Get Create (RefClass(cComChilkatHttpRequest)) To hoReqB
    If (Not(IsComObjectCreated(hoReqB))) Begin
        Send CreateComObject of hoReqB
    End
    Set ComHttpVerb Of hoReqB To "POST"
    Set ComPath Of hoReqB To "/api/token"
    Set ComContentType Of hoReqB To "application/x-www-form-urlencoded"
    Send ComAddParam To hoReqB "grant_type" "client_credentials"
    Send ComAddParam To hoReqB "resource" "https://xyz.softeon.com/resources"
    Send ComAddParam To hoReqB "scope" "openid"
    Send ComAddParam To hoReqB "client_id" "AAA"
    Send ComAddParam To hoReqB "client_secret" "BBB"

    Get Create (RefClass(cComChilkatHttpResponse)) To hoRespB
    If (Not(IsComObjectCreated(hoRespB))) Begin
        Send CreateComObject of hoRespB
    End
    Get pvComObject of hoReqB to vReqB
    Get pvComObject of hoRespB to vRespB
    Get ComHttpReq Of hoHttpB "https://xyz.softeon.com/api/token" vReqB vRespB To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttpB To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Response body:"
    Get ComBodyStr Of hoRespB To sTemp1
    Showln sTemp1

    // Looking at the sessionLogB.txt, we can see the following HTTP POST was sent:

    // POST /api/token HTTP/1.1
    // Host: xyz.softeon.com
    // Content-Type: application/x-www-form-urlencoded
    // Content-Length: 134
    // 
    // grant_type=client_credentials&resource=https%3A%2F%2Fxyz.softeon.com%2Fresources&scope=openid&client_id=AAA&client_secret=BBB

    // --
    // Notice how the params are sent in the body of the request.  The Content-Length is now non-zero, and the params are not present in the start-line of the HTTP request.
    // Technically, this SHOULD be equivalent to the above request where the params are in the start-line.
    // Unfortunately, many servers are brittle and stupid and want the POST to be in a particular form.


End_Procedure