Android Question POST request multipart always getting internal server error

Devv

Active Member
Licensed User
Longtime User
I am trying to upload file to a web server

the post is working perfectly on rest api testing software


but the same parameters are not working on my app

here is the code

B4X:
    Dim j As HttpJob
    Dim img As String = "btn1.jpg"
    j.Initialize("", Me)
    Dim mp As MultipartFileData
    mp.Initialize
    mp.Dir = File.DirAssets
    mp.FileName = img
    mp.KeyName = "Driving_licence"
    mp.ContentType = "image/png"
    j.PostMultipart("http://driver.com/taxi/api/customerProfile", Null, Array(mp))
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
        If img = j.GetString Then
            ToastMessageShow("done..", False)
        Else
            ToastMessageShow("Error ", False)
        End If
    End If
    j.Release


no matter how many examples i tried i always get job failure with "internal server error page"
the web developer is saying that this client error and it is working fine with postman

here is the php code

PHP:
if(isset($_FILES)) {
foreach ( $_FILES as $fName => $fArray ) {
$photoName = $_FILES["file"]["name"];
move( public_path( 'avatars' ), $photoName );
}
}


any help/ideas are highly appreciated i am stuck for more that 10 hours
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What parameters did you use? Hard to duplicate something if we don’t know the exact inputs/parameters used with another tool.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Your posted PHP code should not work with your screenshot of your testing API. According to the screenshot, your PHP code
B4X:
$photoName = $_FILES["file"]["name"];
should be
B4X:
$photoName = $_FILES["Driving_license"]["name"];
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Using http://ptsv2.com, the only thing really different between what Insomnia (https://insomnia.rest/) creates and what B4A creates is that B4A sets the Content-Encoding header (to UTF8). When I add that header via Insomnia, your site bombs out just like with B4A. So if you add
B4X:
j.GetRequest.RemoveHeaders("Content-Encoding")
after you j.PostMultipart line, j.PostMultipart works.
Note 1: RemoveHeaders method relies on the OkHttp library
Note 2: @Devv sent me his Insomnia configuration. There are two additional parameters that are required to do a successful post, but without these two, one still receives a JSON structure with the error message. With the Content-Encoding header set though, the site just bombs out, no matter what values are posted.
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User


thanks for your replay

i made as you suggested but still getting the internal server error

i PMed you the details please advise
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
@OliverA
thank you so much for providing this working code, if you may explain the error i was making, because i still dont understand

B4X:
    Dim j As HttpJob
    Dim img As String = "tyson.jpg"
    j.Initialize("", Me)
    Dim mp As MultipartFileData
    mp.Initialize
    mp.Dir = File.DirAssets
    mp.FileName = img
    mp.KeyName = "Driving_licence"
    mp.ContentType = "image/jpg"
    Dim postValues As Map
    postValues.Initialize
    postValues.Put("mobile_number", "1981516")
    postValues.Put("otp", "0142")
    Dim postList As List
    postList.Initialize
    postList.Add(mp)
    j.PostMultipart("http://driver.com/taxi/api/customerProfile", postValues, postList)
    j.GetRequest.RemoveHeaders("Content-Encoding")
    'j.PostMultipart("http://ptsv2.com/t/7jh2c-1540092492/post", postValues, Array(mp))
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
        If img = j.GetString Then

        Else

        End If
    Else
        Log(j.GetString)
        Log("What?")
        Log(j.ErrorMessage)

    End If
    j.Release
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Please get rid of JobDone sub. No clue why you are including it, since you’re using Wait For
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…