Android Question How send header parameters in PostMultipart

Discussion in 'Android Questions' started by scsjc, Aug 6, 2019.

  1. scsjc

    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"

    Code:
    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
     
  2. DonManfred

    DonManfred Expert Licensed User

    Code:
    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 and Erel like this.
  3. scsjc

    scsjc Well-Known Member Licensed User

    Thanks... work the parametres
     
  4. scsjc

    scsjc Well-Known Member Licensed User


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

    DonManfred Expert Licensed User

    I don´t understand the question
     
  6. scsjc

    scsjc Well-Known Member Licensed User

    i'm try to send a file in a post.... the file in a post is in the body no?
     
  7. DonManfred

    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.
     
  8. scsjc

    scsjc Well-Known Member Licensed User

    thanks !!!!
     
  9. MarkusR

    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)
     
    scsjc likes this.
  10. scsjc

    scsjc Well-Known Member Licensed User

    have you a code sample?...
    my knowledge about b4j is low
     
  11. MarkusR

    MarkusR Well-Known Member Licensed User

    sure :)
    Snap_2019.08.06_16h08m38s_001.png

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

    Code:
    #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 StringAs 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(1024As 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
    Code:
    <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: Aug 6, 2019
    scsjc likes this.
  12. scsjc

    scsjc Well-Known Member Licensed User

    great solution ;)
     
    MarkusR likes this.
  13. MarkusR

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice