POST request multipart/form-data with HttpUtils2

ziomorgan

Active Member
Licensed User
Longtime User
Please Erel...POST request multipart/form-data with HttpUtils2

I must send following post request with httputils2 but i receive always

Error: Not Acceptable: Unknown or unsupported response format.

:BangHead::BangHead::BangHead:

HTML:
POST /restsrv/user/login HTTP/1.1
User-Agent: curl/7.28.1
Host: petforce1.com
Accept: */*
Content-Length: 258
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------aa5d15c24f81

------------------------------aa5d15c24f81
Content-Disposition: form-data; name="username"

pablo
------------------------------aa5d15c24f81
Content-Disposition: form-data; name="password"

picassopwd
------------------------------aa5d15c24f81--

Here Activity_Create source code:
B4X:
Dim job2 As HttpJob
Dim NV As Map

NV.Initialize
NV.Put("username", "pablo")
NV.Put("password", "picassopwd")
'Send a POST request
job2.Initialize("Job2", Me)
job2.PostMultipart("http://www.xxxxxxxx.com/restsrv/user/login", NV, Null)

Here sub PostMultipart
B4X:
Public Sub PostMultipart(Link As String, NameValues As Map, Files As List) As HttpRequest
   Dim boundary As String
   Dim stream As OutputStream
   Dim EOL As String
   Dim b() As Byte
   
   boundary = "------------------------------6560d8ff34d2"
   stream.InitializeToBytesArray(20)
   
   EOL = Chr(13) & Chr(10) 'CRLF constant matches Android end of line character which is chr(10).
   If NameValues <> Null AND NameValues.IsInitialized Then
      'Write the name/value pairs
      Dim key, value As String
      For i = 0 To NameValues.Size - 1
         key = NameValues.GetKeyAt(i)
         value = NameValues.GetValueAt(i)
         b = ("--" & boundary & EOL & "Content-Disposition: form-data; name=" _ 
            & QUOTE & key & QUOTE & EOL & EOL & value & EOL).GetBytes("UTF8")
         stream.WriteBytes(b, 0, b.Length)
      Next
   End If
   If Files <> Null AND Files.IsInitialized Then
      'write the files
      Dim FD As FileData
      For i = 0 To Files.Size - 1
         FD = Files.Get(i)
         b = ("--" & boundary & EOL & "Content-Disposition: form-data; name=" _ 
            & QUOTE & FD.KeyName & QUOTE & "; filename=" & QUOTE & FD.FileName & QUOTE _
            & EOL & "Content-Type: " & EOL & FD.ContentType & EOL & EOL).GetBytes("UTF8")
         stream.WriteBytes(b, 0, b.Length)
         Dim In As InputStream
         In = File.OpenInput(FD.Dir, FD.FileName)
         File.Copy2(In, stream) 'read the file and write it to the stream
         b = EOL.GetBytes("UTF8")
         stream.WriteBytes(b, 0, b.Length)
      Next
   End If
   b = (EOL & "--" & boundary & "--" & EOL).GetBytes("UTF8")
   stream.WriteBytes(b, 0, b.Length)
   stream.Flush 
   stream.Close
   b = stream.ToBytesArray

   Dim bc As ByteConverter

   Log(bc.StringFromBytes(b,"UTF8"))
   
   PostBytesMultipart(Link, b, boundary)
End Sub
 
Last edited:

ziomorgan

Active Member
Licensed User
Longtime User
I'm sorry, I thought to open a new post with all the information at my disposal, including source code and code call.
If you want I copy these infrmazioni in the post and this close.
thanks
 
Upvote 0

ziomorgan

Active Member
Licensed User
Longtime User
Hi Erel,

hc_ResponseError StatusCode=406

Do you have any ideas what is the problem ?

Thanks
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I thought I've told you about this error in your other thread. As Erel mentioned, it's not very good to handling multiple threads for the same question.
 
Last edited:
Upvote 0

ziomorgan

Active Member
Licensed User
Longtime User
For Erel:
Ok, this is a start point (thank you for this).
I have a working post request (as mentioned at post #1) but i don't know how i can retrieve a complete post request made by b4a.
 
Upvote 0

ziomorgan

Active Member
Licensed User
Longtime User
Yes, the playload is the same.
How can i set the headers ?
(the working request is attached at the post #1).
Thanks
 
Upvote 0

ziomorgan

Active Member
Licensed User
Longtime User
THANK YOU VERY MUCH EREL,
thanks for your valuable information I managed to solve:
enough to enter the type of requests accepted by the server

B4X:
job2.GetRequest.SetHeader ("Accept", "*/*")
 
Upvote 0
Top