Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi DLL) POST application/x-www-form-urlencoded Two WaysThis 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..
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, HttpRequest, HttpResponse; ... procedure TForm1.Button1Click(Sender: TObject); var http: HCkHttp; resp: HCkHttpResponse; httpB: HCkHttp; reqB: HCkHttpRequest; respB: HCkHttpResponse; begin // 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: http := CkHttp_Create(); // We can see the exact HTTP request sent by setting a session log filename: CkHttp_putSessionLogFilename(http,'qa_output/sessionLog.txt'); CkHttp_SetRequestHeader(http,'Content-Type','application/x-www-form-urlencoded'); resp := CkHttp_QuickRequest(http,'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'); if (CkHttp_getLastMethodSuccess(http) = False) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); Exit; end; Memo1.Lines.Add('Response body:'); Memo1.Lines.Add(CkHttpResponse__bodyStr(resp)); CkHttpResponse_Dispose(resp); // 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.) httpB := CkHttp_Create(); CkHttp_putSessionLogFilename(httpB,'qa_output/sessionLogB.txt'); reqB := CkHttpRequest_Create(); CkHttpRequest_putHttpVerb(reqB,'POST'); CkHttpRequest_putPath(reqB,'/api/token'); CkHttpRequest_putContentType(reqB,'application/x-www-form-urlencoded'); CkHttpRequest_AddParam(reqB,'grant_type','client_credentials'); CkHttpRequest_AddParam(reqB,'resource','https://xyz.softeon.com/resources'); CkHttpRequest_AddParam(reqB,'scope','openid'); CkHttpRequest_AddParam(reqB,'client_id','AAA'); CkHttpRequest_AddParam(reqB,'client_secret','BBB'); respB := CkHttp_PostUrlEncoded(httpB,'https://xyz.softeon.com/api/token',reqB); if (CkHttp_getLastMethodSuccess(httpB) = False) then begin Memo1.Lines.Add(CkHttp__lastErrorText(httpB)); Exit; end; Memo1.Lines.Add('Response body:'); Memo1.Lines.Add(CkHttpResponse__bodyStr(respB)); CkHttpResponse_Dispose(respB); // 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. CkHttpResponse_Dispose(respB); CkHttp_Dispose(http); CkHttp_Dispose(httpB); CkHttpRequest_Dispose(reqB); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.