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
(PureBasic) REST Stream Multipart Body from FileDemonstrates how to send a multipart/form-data HTTP request, where one of the parts contains data streamed directly from a file. This is good for cases where the file to be uploaded is very large.
IncludeFile "CkStream.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkRest.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. rest.i = CkRest::ckCreate() If rest.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Connect to the destination web server. bTls.i = 1 port.i = 443 bAutoReconnect.i = 1 success.i = CkRest::ckConnect(rest,"www.somewebserver.com",port,bTls,bAutoReconnect) ; This example will send the following multipart/form-data request. ; The Content-Length is automatically computed and added by Chilkat. ; POST /some_path HTTP/1.1 ; Content-Type: multipart/form-data; boundary=---------------------------735323031399963166993862150 ; Content-Length: 834 ; ; -----------------------------735323031399963166993862150 ; Content-Disposition: form-data; name="text1" ; ; text 123 abc ; -----------------------------735323031399963166993862150 ; Content-Disposition: form-data; name="text2" ; ; xyz ; -----------------------------735323031399963166993862150 ; Content-Disposition: form-data; name="file1"; filename="a.txt" ; Content-Type: text/plain ; ; Content of a.txt. ; ; -----------------------------735323031399963166993862150 ; Content-Disposition: form-data; name="file2"; filename="a.html" ; Content-Type: text/html ; ; <!DOCTYPE html><title>Content of a.html.</title> ; ; -----------------------------735323031399963166993862150 ; Content-Disposition: form-data; name="file3"; filename="starfish.jpg" ; Content-Type: image/jpeg ; ; binary data goes here ; -----------------------------735323031399963166993862150-- ; Set the Content-Type for the topmost MIME part. CkRest::ckAddHeader(rest,"Content-Type","multipart/form-data") ; Specify each part of the request. CkRest::setCkPartSelector(rest, "1") CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "text1" + Chr(34)) CkRest::ckSetMultipartBodyString(rest,"text 123 abc") CkRest::setCkPartSelector(rest, "2") CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "text2" + Chr(34)) CkRest::ckSetMultipartBodyString(rest,"xyz") CkRest::setCkPartSelector(rest, "3") CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "file1" + Chr(34) + "; filename=" + Chr(34) + "a.txt" + Chr(34)) CkRest::ckAddHeader(rest,"Content-Type","text/plain") CkRest::ckSetMultipartBodyString(rest,"Content of a.txt.") CkRest::setCkPartSelector(rest, "4") CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "file2" + Chr(34) + "; filename=" + Chr(34) + "a.html" + Chr(34)) CkRest::ckAddHeader(rest,"Content-Type","text/html") sbHtml.i = CkStringBuilder::ckCreate() If sbHtml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStringBuilder::ckLoadFile(sbHtml,"qa_data/html/a.html","utf-8") CkRest::ckSetMultipartBodySb(rest,sbHtml) CkRest::setCkPartSelector(rest, "5") CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "file3" + Chr(34) + "; filename=" + Chr(34) + "starfish.jpg" + Chr(34)) CkRest::ckAddHeader(rest,"Content-Type","image/jpeg") ; When the request is sent, stream this part directly from the file. ; This avoids having to load the entire file into memory. fileStream.i = CkStream::ckCreate() If fileStream.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStream::setCkSourceFile(fileStream, "qa_data/jpg/starfish.jpg") CkRest::ckSetMultipartBodyStream(rest,fileStream) responseBody.s = CkRest::ckFullRequestMultipart(rest,"POST","/some_path") If CkRest::ckLastMethodSuccess(rest) <> 1 Debug CkRest::ckLastErrorText(rest) CkRest::ckDispose(rest) CkStringBuilder::ckDispose(sbHtml) CkStream::ckDispose(fileStream) ProcedureReturn EndIf ; ... ; ... ; Clear the REST object for any subsequent requests.. CkRest::ckClearAllHeaders(rest) CkRest::ckClearAllParts(rest) CkRest::setCkPartSelector(rest, "") CkRest::ckDispose(rest) CkStringBuilder::ckDispose(sbHtml) CkStream::ckDispose(fileStream) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.