B4J Question Multipart Post - boundary issue

avalle

Active Member
Licensed User
Longtime User
Hi,
while trying to implement multipart post I've found a possible issue but I wanted to check with the community before logging a bug.
The code I'm using is pretty standard, as found in multiple existing threads:

Code: Copy
Dim job As HttpJob
Dim ret As String = "ERROR"

job.Initialize("job", Me)
job.PostMultipart(URL, Null, Array(fd))
job.GetRequest.SetHeader("User-Agent", useragent)
job.GetRequest.SetContentType("multipart/form-data")
job.GetRequest.SetHeader("Authorization", auth)
Wait For (job) JobDone(job As HttpJob)

If job.Success Then
    ret = job.GetString
End If
job.Release
Return ret

However, by sniffing the network traffic, I noticed that the Content-type header that is generated by B4J is missing the boundary element, thus the request fails on the server end as the file post is not recognized to be part of my multipart Post.

This B4J app would generate the following, and fail:
Code: Copy
Content-type: multipart/form-data
--------------------------1461124740692
Content-Disposition: form-data; name="parameters"; filename="Params.json"
Content-Type: application/json
{ ... }

A working session with cURL would instead generate the following:
Code: Copy
Content-type: multipart/form-data; boundary=------------------------xyz1234abcdef
--------------------------xyz1234abcdef
Content-Disposition: form-data; name="parameters"; filename="Params.json"
Content-Type: application/json
{ ... }

The "boundary=------------------------1461124740692" is not added by B4J to the multipart/form-data Content-type, while in cURL the boundary string is automatically generated.
So in order to make it work, my code needs to be changed to this:

Code: Copy
Dim job As HttpJob
Dim ret As String = "ERROR"

job.Initialize("job", Me)
job.PostMultipart(URL, Null, Array(fd))
job.GetRequest.SetHeader("User-Agent", useragent)
job.GetRequest.SetContentType("multipart/form-data; boundary=---------------------------1461124740692")
job.GetRequest.SetHeader("Authorization", auth)
Wait For (job) JobDone(job As HttpJob)

If job.Success Then
    ret = job.GetString
End If
job.Release
Return ret

Can someone confirm my assumption that B4J should generate a random boundary string autonomously instead of having to add the magic string "---------------------------1461124740692" in my code?

Thanks
Andrea
 
Thanks Erel.
Now I see the code in HttpJob and that explains everything...
And now I also understand why the order of the various calls to job.methodxyz is important.

However, this means that if I need to use a different Content-type from multipart/form-data (for example I have to use multipart/mixed or multipart/related) then the only option I have is to customize the jOKHttpUtil library. Correct?
If so, then the question is where do I find the newest version of its source code?
The latest I could find is v2.7 but the current is 2.91.
I don't see it in the Github repo.

Thanks
Andrea
 
Upvote 0
Back
Top