Sample code for 30+ languages & platforms
B4X

REST File Upload (multipart/form-data)

See more REST Examples

Demonstrates how to upload a file using multipart/form-data.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

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

Dim rest As ChilkatRest
rest.Initialize("rest")

'  Connect to the HTTP server using TLS.
Dim bTls As Boolean = True
Dim port As Int = 443
Dim bAutoReconnect As Boolean = True
'  Make sure to replace "www.chilkatsoft.com" with your domain..
success = rest.Connect("www.chilkatsoft.com", port, bTls, bAutoReconnect)
If success <> True Then
    Log(rest.LastErrorText)
    Return
End If


Dim fileStream As ChilkatStream
fileStream.Initialize("fileStream")
fileStream.SourceFile = "qa_data/jpg/starfish.jpg"

rest.AddHeader("Content-Type", "multipart/form-data")

rest.PartSelector = "1"
rest.AddHeader("Content-Type", "image/jpg")
rest.AddHeader("Content-Disposition", "form-data; name=" & Chr(34) & "filename" & Chr(34) & "; filename=" & Chr(34) & "starfish.jpg" & Chr(34))
rest.SetMultipartBodyStream(fileStream)
rest.PartSelector = "0"

Dim responseBody As String = rest.FullRequestMultipart("POST", "/the_uri_path_to_receive_the_upload")
If rest.LastMethodSuccess <> True Then
    Log(rest.LastErrorText)
    Return
End If


If rest.ResponseStatusCode <> 200 Then
    Log("Received error response code: " & rest.ResponseStatusCode)
    Return
End If


Log("File uploaded")