Android Question How send header parameters in PostMultipart

scsjc

Well-Known Member
Licensed User
I need send a image with a Post
In a Headers need put a Parameters HASH & USER

i try this code bad dont work.... message error: "User not supplied"

B4X:
        Dim j As HttpJob
        j.Initialize("", Me)
        Dim fd As MultipartFileData
        fd.Initialize
        fd.KeyName = "file"
        fd.Dir = File.DirInternal
        fd.FileName = "file.jpg"
        fd.ContentType = "image/jpg"
        j.PostMultipart(posturl, CreateMap("hash": hash,"user": nick), Array(fd))
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log(j.GetString)
        End If
        j.Release
 

DonManfred

Expert
Licensed User
B4X:
Dim j As HttpJob
        j.Initialize("", Me)
        Dim fd As MultipartFileData
        fd.Initialize
        fd.KeyName = "file"
        fd.Dir = File.DirInternal
        fd.FileName = "file.jpg"
        fd.ContentType = "image/jpg"
        j.PostMultipart(posturl, CreateMap("hash": hash,"user": nick), Array(fd))
        j.GetRequest.SetHeader("Authorization", "Bearer "&mAccessToken) ' or whatever you need to set. Do it here
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log(j.GetString)
        End If
        j.Release
 

scsjc

Well-Known Member
Licensed User
B4X:
Dim j As HttpJob
        j.Initialize("", Me)
        Dim fd As MultipartFileData
        fd.Initialize
        fd.KeyName = "file"
        fd.Dir = File.DirInternal
        fd.FileName = "file.jpg"
        fd.ContentType = "image/jpg"
        j.PostMultipart(posturl, CreateMap("hash": hash,"user": nick), Array(fd))
        j.GetRequest.SetHeader("Authorization", "Bearer "&mAccessToken) ' or whatever you need to set. Do it here
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log(j.GetString)
        End If
        j.Release
Thanks... work the parametres
 

scsjc

Well-Known Member
Licensed User
B4X:
Dim j As HttpJob
        j.Initialize("", Me)
        Dim fd As MultipartFileData
        fd.Initialize
        fd.KeyName = "file"
        fd.Dir = File.DirInternal
        fd.FileName = "file.jpg"
        fd.ContentType = "image/jpg"
        j.PostMultipart(posturl, CreateMap("hash": hash,"user": nick), Array(fd))
        j.GetRequest.SetHeader("Authorization", "Bearer "&mAccessToken) ' or whatever you need to set. Do it here
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log(j.GetString)
        End If
        j.Release

another question about .... this code send the file in a body ... ???
 

DonManfred

Expert
Licensed User
The file is in the body in the request, yes.
For the server it is like sending a file with a formular.
 

MarkusR

Well-Known Member
Licensed User
you can see the real data of if you connect to a b4j app which listen & let connect at a tcp socket port 80. (http)
 

MarkusR

Well-Known Member
Licensed User
have you a code sample?...
my knowledge about b4j is low
sure :)
Snap_2019.08.06_16h08m38s_001.png

if port 80 is in use, just use 81 or other free one and call
B4X:
IP:Port
in html form

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
 
    'https://www.b4x.com/b4j/help/jnetwork.html#serversocket
    Private ss As ServerSocket
 
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
 
    ss.Initialize(80,"ss") 'Port must be free and not used by any WebServer like MS IIS,Apache,Jetty
    'ss.InitializeSSL
    ss.Listen
 
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub ss_NewConnection(Successful As Boolean, NewSocket As Socket)

    'https://www.w3schools.com/tags/tag_form.asp
 
    Dim st As InputStream
    st = NewSocket.InputStream
 
    Dim buffer(1024) As Byte
    Dim count As Int =0
    Do Until count = -1
        count = st.ReadBytes(buffer, 0, buffer.length)  
        If count > 0 Then Log(BytesToString(buffer,0, count , "UTF-8"))
    Loop
 
End Sub
form.html edit with notepad and you can open with browser from desktop
B4X:
<html>
<body>
 <form action="http://127.0.0.1/upload" enctype="multipart/form-data" method="post">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>
 
Last edited:

scsjc

Well-Known Member
Licensed User
sure :)
View attachment 82909

if port 80 is in use, just use 81 or other free one and call
B4X:
IP:Port
in html form

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
 
    'https://www.b4x.com/b4j/help/jnetwork.html#serversocket
    Private ss As ServerSocket
 
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
 
    ss.Initialize(80,"ss") 'Port must be free and not used by any WebServer like MS IIS,Apache,Jetty
    'ss.InitializeSSL
    ss.Listen
 
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub ss_NewConnection(Successful As Boolean, NewSocket As Socket)

    'https://www.w3schools.com/tags/tag_form.asp
 
    Dim st As InputStream
    st = NewSocket.InputStream
 
    Dim buffer(1024) As Byte
    Dim count As Int =0
    Do Until count = -1
        count = st.ReadBytes(buffer, 0, buffer.length) 
        If count > 0 Then Log(BytesToString(buffer,0, count , "UTF-8"))
    Loop
 
End Sub
form.html edit with notepad and you can open with browser from desktop
B4X:
<html>
<body>
 <form action="http://127.0.0.1/upload" enctype="multipart/form-data" method="post">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>
great solution ;)
 

MarkusR

Well-Known Member
Licensed User
i think it is really important so see what data is tranfered for a better understanding of a http request.
 
Top