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 Send multipart/form-dataDemonstrates how to send a multipart/form-data HTTP request.
IncludeFile "CkBinData.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. ; It will be different than what is shown here. ; The boundary string is also generated by Chilkat and will be different ; than shown below. ; 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") jpgBytes.i = CkBinData::ckCreate() If jpgBytes.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkBinData::ckLoadFile(jpgBytes,"qa_data/jpg/starfish.jpg") CkRest::ckSetMultipartBodyBd(rest,jpgBytes) responseBody.s = CkRest::ckFullRequestMultipart(rest,"POST","/some_path") If CkRest::ckLastMethodSuccess(rest) <> 1 Debug CkRest::ckLastErrorText(rest) CkRest::ckDispose(rest) CkStringBuilder::ckDispose(sbHtml) CkBinData::ckDispose(jpgBytes) 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) CkBinData::ckDispose(jpgBytes) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.