Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Unicode C++ Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Unicode C++) HTTPS multipart/form-data POST

Demonstrates how to send a multipart/form-data POST over HTTPS (using TLS).

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <CkHttpRequestW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>

void ChilkatSample(void)
    {
    // This example assumes the Chilkat HTTP API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example demonstrates how to send a multipart/form-data POST that
    // looks like this:

    // 	POST /cgi/XXX.pl HTTP/1.0
    // 	Accept: text/html
    // 	Connection: Keep-Alive
    // 	User-Agent: XXX/8.0.15
    // 	Content-type: multipart/form-data, boundary=XXXxyxy
    // 	Content-Length: 682
    // 
    // 	--XXXxyxy
    // 	content-disposition: form-data; name="UploadAgent"
    // 
    // 	InterfaceVersion1.5
    // 	--XXXxyxy
    // 	content-disposition: form-data; name="user"
    // 
    // 	userValue
    // 	--XXXxyxy
    // 	content-disposition: form-data; name="password"
    // 
    // 	passwordValue
    // 	--XXXxyxy
    // 	content-disposition: form-data; name="file"
    // 
    // 	fileValue
    // 	--XXXxyxy
    // 	content-disposition: form-data; name="data_version"
    // 
    // 	dataVersion
    // 	--XXXxyxy
    // 	content-disposition: form-data; name="content2"; filename="XXX"
    // 
    // 	THE FILE CONTENT GOES HERE...
    // 	--XXXxyxy--
    // 

    // First, let's build the HTTP request object
    CkHttpRequestW req;

    req.put_HttpVerb(L"POST");
    req.put_Path(L"/cgi/XXX.pl");

    // The boundary string is automatically generated and added by Chilkat.
    // The value for the boundary string doesn't matter. (As long as it's a unique string that doesn't occur elsewhere in the request.)
    req.put_ContentType(L"multipart/form-data");

    // Adding the Connection: Keep-Alive is optional.  It only makes sense if the intent is to send
    // additional requests to the same domain (your-namespace-sb.accesscontrol.windows.net) within a reasonable time period.
    req.AddHeader(L"Connection",L"Keep-Alive");

    // --------------------------------------------------
    // IMPORTANT: Never set the Content-Length header.  
    // Chilkat will automatically compute the correct Content-Length and will add it.
    // --------------------------------------------------

    // If a specific User-Agent header field is needed, it can be added by calling AddHeader.
    req.AddHeader(L"User-Agent",L"XXX/8.0.15");

    // The "Accept" header, if present, tells the server what Content-Type responses will be accepted.
    // In this case, we're telling the server that we'll only accept "text/html" responses, and therefore
    // the server SHOULD only send a text/html response.  Technically, the Accept header is not required.
    req.AddHeader(L"Accept",L"text/html");

    // Add the params to the request.  Given that the Content-Type is set to "multipart/form-data", when
    // Chilkat composes the request, it will put each param in it's own MIME sub-part (i.e. in it's own
    // part delimited by the boundary string).
    req.AddParam(L"UploadAgent",L"InterfaceVersion1.5");
    req.AddParam(L"user",L"userValue");
    req.AddParam(L"password",L"passwordValue");
    req.AddParam(L"file",L"fileValue");
    req.AddParam(L"data_version",L"dataVersion");

    // The last param is the contents of a file.
    // If it's a file on disk, we can add it like this:
    const wchar_t *pathToFileOnDisk = L"c:/someDir/someFile.dat";
    bool success = req.AddFileForUpload(L"content2",pathToFileOnDisk);
    if (success != true) {
        wprintf(L"%s\n",req.lastErrorText());
        return;
    }

    // Alternatively, if the contents of the file are in memory, perhaps in a string
    // variable, the file can be added like this instead.
    const wchar_t *fileContents = L"This is the content of the file being uploaded.";
    success = req.AddStringForUpload(L"content2",L"XXX",fileContents,L"utf-8");

    // -----------------------------------------------------------
    // IMPORTANT: To duplicate the HTTP request shown above, you'll want to choose 
    // either AddStringForUpload or AddFileForUpload, but not both.  It's possible to upload
    // any number of files by calling AddStringForUpload and/or AddFileForUpload any number
    // of times, once per file to be uploaded.  This of course assumes that the receiving
    // end is programmed to receive multiple files..
    // ------------------------------------------------------------

    CkHttpW http;

    // The request is ready... now send it using HTTPS (which is port 443 by default).

    CkHttpResponseW *resp = http.SynchronousRequest(L"www.myserver.com",443,true,req);
    if (http.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    wprintf(L"HTTP response status: %d\n",resp->get_StatusCode());

    // In this case, the response would be HTML because our Accept header
    // told the server to only return HTML.  The HTML is available on the BodyStr
    // property of the response object:
    const wchar_t *htmlStr = resp->bodyStr();
    wprintf(L"Received:\n");
    wprintf(L"%s\n",htmlStr);

    delete resp;
    }

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.