I have found something in the [B4X] OkHttpUtils2 / iHttpUtils2 / HttpUtils2 source code that confuses me. I think it might be broken, or perhaps I'm missing something obvious in the logic.
The issue is with the boolean called empty. Its only use is for the sub MultipartStartSection. It is initially set to True, but after the first call to MultipartStartSection it gets set to False and that never changes anywhere. I can't help but think that an empty = True should be added somewhere, perhaps just before the (potential) files are added?
This is an exact copy from the file HttpJob.bas in the file B4A_Modules.zip where I have added comments to almost all lines with the variable empty.
The issue is with the boolean called empty. Its only use is for the sub MultipartStartSection. It is initially set to True, but after the first call to MultipartStartSection it gets set to False and that never changes anywhere. I can't help but think that an empty = True should be added somewhere, perhaps just before the (potential) files are added?
This is an exact copy from the file HttpJob.bas in the file B4A_Modules.zip where I have added comments to almost all lines with the variable empty.
B4X:
'Sends a multipart POST request.
'NameValues - A map with the keys and values. Pass Null if not needed.
'Files - List of MultipartFileData items. Pass Null if not needed.
Public Sub PostMultipart(Link As String, NameValues As Map, Files As List)
Dim boundary As String = "---------------------------1461124740692"
Dim stream As OutputStream
stream.InitializeToBytesArray(0)
Dim b() As Byte
Dim eol As String = Chr(13) & Chr(10)
Dim empty As Boolean = True ' <--- empty is set to True
If NameValues <> Null And NameValues.IsInitialized Then
For Each key As String In NameValues.Keys
Dim value As String = NameValues.Get(key)
empty = MultipartStartSection (stream, empty) ' <--- empty is set to False
Dim s As String = _
$"--${boundary}
Content-Disposition: form-data; name="${key}"
${value}"$
b = s.Replace(CRLF, eol).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
Next
End If
If Files <> Null And Files.IsInitialized Then
For Each fd As MultipartFileData In Files
empty = MultipartStartSection (stream, empty) ' <--- empty is set to False
Dim s As String = _
$"--${boundary}
Content-Disposition: form-data; name="${fd.KeyName}"; filename="${fd.FileName}"
Content-Type: ${fd.ContentType}
"$
b = s.Replace(CRLF, eol).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
Dim in As InputStream = File.OpenInput(fd.Dir, fd.FileName)
File.Copy2(in, stream)
Next
End If
empty = MultipartStartSection (stream, empty) ' <--- empty is set to False
s = _
$"--${boundary}--
"$
b = s.Replace(CRLF, eol).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
PostBytes(Link, stream.ToBytesArray)
req.SetContentType("multipart/form-data; boundary=" & boundary)
req.SetContentEncoding("UTF8")
End Sub
Private Sub MultipartStartSection (stream As OutputStream, empty As Boolean) As Boolean
If empty = False Then
stream.WriteBytes(Array As Byte(13, 10), 0, 2)
Else
empty = False
End If
Return empty ' <--- empty is always returned as False
End Sub