Italian POST request multipart/form-data with HttpUtils2

ziomorgan

Active Member
Licensed User
Longtime User
POST richiesta multipart/form-data con HttpUtils2

Devo inviare la richiesta POST con httputils2 ma ricevo sempre errore:

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--

Activity_Create:
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.xxxxxxxxx.com/restsrv/user/login", NV, Null)

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:
Top