Sample code for 30+ languages & platforms
PowerBuilder

REST Send multipart/form-data

See more REST Examples

Demonstrates how to send a multipart/form-data HTTP request.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_Port
integer li_BAutoReconnect
oleobject loo_SbHtml
oleobject loo_JpgBytes
string ls_ResponseBody

li_Success = 0

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

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Connect to the destination web server.
li_BTls = 1
li_Port = 443
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("www.somewebserver.com",li_Port,li_BTls,li_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.
loo_Rest.AddHeader("Content-Type","multipart/form-data")

// Specify each part of the request.
loo_Rest.PartSelector = "1"
loo_Rest.AddHeader("Content-Disposition","form-data; name=~"text1~"")
loo_Rest.SetMultipartBodyString("text 123 abc")

loo_Rest.PartSelector = "2"
loo_Rest.AddHeader("Content-Disposition","form-data; name=~"text2~"")
loo_Rest.SetMultipartBodyString("xyz")

loo_Rest.PartSelector = "3"
loo_Rest.AddHeader("Content-Disposition","form-data; name=~"file1~"; filename=~"a.txt~"")
loo_Rest.AddHeader("Content-Type","text/plain")
loo_Rest.SetMultipartBodyString("Content of a.txt.")

loo_Rest.PartSelector = "4"
loo_Rest.AddHeader("Content-Disposition","form-data; name=~"file2~"; filename=~"a.html~"")
loo_Rest.AddHeader("Content-Type","text/html")
loo_SbHtml = create oleobject
li_rc = loo_SbHtml.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbHtml.LoadFile("qa_data/html/a.html","utf-8")
loo_Rest.SetMultipartBodySb(loo_SbHtml)

loo_Rest.PartSelector = "5"
loo_Rest.AddHeader("Content-Disposition","form-data; name=~"file3~"; filename=~"starfish.jpg~"")
loo_Rest.AddHeader("Content-Type","image/jpeg")
loo_JpgBytes = create oleobject
li_rc = loo_JpgBytes.ConnectToNewObject("Chilkat.BinData")

loo_JpgBytes.LoadFile("qa_data/jpg/starfish.jpg")
loo_Rest.SetMultipartBodyBd(loo_JpgBytes)

ls_ResponseBody = loo_Rest.FullRequestMultipart("POST","/some_path")
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_SbHtml
    destroy loo_JpgBytes
    return
end if

// ...
// ...

// Clear the REST object for any subsequent requests..
loo_Rest.ClearAllHeaders()
loo_Rest.ClearAllParts()
loo_Rest.PartSelector = ""


destroy loo_Rest
destroy loo_SbHtml
destroy loo_JpgBytes