Android Question PostMultipart and https

msucho

Member
Licensed User
Longtime User
Hi
I try to upload a file using the HttpJob Post Multipart method.
It works fine with "http" but always fails with "https" (using the same server and php script),
Debugin php errors the array $_FILES has valid data on http but it is always empty using https.
Using an html form to upload files the php script works correctly for both modes.

Thank You for your help.


B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    LogColor("Start Download",Colors.Yellow)
    Dim file_local ="testmp.gif" As String
    Log(File.Combine(File.DirInternal,file_local))
    Log(File.Size(File.DirInternal,file_local))
    LogColor("Start Upload",Colors.Yellow)
    Dim link="https://mydomain.com/upload.php" As String
    Dim j As HttpJob
    Dim files As List
    files.Initialize
    j.Initialize("", Me)
    Dim mp As MultipartFileData
    mp.Initialize
    mp.Dir = File.DirInternal
    mp.FileName = file_local
    mp.KeyName = "file"
    mp.ContentType="multipart/form-data"
    files.Add(mp)
    j.PostMultipart(link, CreateMap("action":"test"), files)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("data: "&j.GetString)
    End If
    j.Release
    LogColor("End Upload",Colors.Yellow)
End Sub

PHP SCRIPT : upload.php

PHP:
<?php
error_reporting(E_ALL); // Enable Errors
    $file_path = "archivos/parasubir/";
    echo $_FILES['file']['name'];
    $file_path = $file_path . basename( $_FILES['file']['name']);
    if(move_uploaded_file($_FILES['file']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "fail";
    }
?>
 
Last edited:

GeoT

Active Member
Licensed User
Longtime User
Hi msucho.

It may be due to several reasons:

SSL Certificate: The server you are trying to send the file to via HTTPS may have problems with its SSL certificate. Make sure the certificate is valid and configured correctly on the server.

Server Configuration: Some servers may require additional configuration to support file uploads over HTTPS. Check the server configuration to ensure it is properly enabled to handle HTTPS requests.


Doesn't it show you any error messages?

Regards.
 
Upvote 0

msucho

Member
Licensed User
Longtime User
Hi msucho.

It may be due to several reasons:

SSL Certificate: The server you are trying to send the file to via HTTPS may have problems with its SSL certificate. Make sure the certificate is valid and configured correctly on the server.

Server Configuration: Some servers may require additional configuration to support file uploads over HTTPS. Check the server configuration to ensure it is properly enabled to handle HTTPS requests.


Doesn't it show you any error messages?

Regards.
Thanks for your answer.
SSL Certificate: issued by letsencrypt.org and works fine with "j.PostString(Link, payload)"

Server Configuration:
it works when I use this basic form:

HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Subir Archivos</title>
</head>
<body>
    <h1>Subir Archivos</h1>
   
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <label for="file">Selecciona un archivo:</label>
        <input type="file" name="file" id="file" required>
        <br>
        <input type="submit" value="Subir Archivo">
    </form>
</body>
</html>

Thnk you
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
it's often difficult to guess why something occurs without being able to test using a poster's code and endpoint(s).
but i do have some questions:

in okhttputils2, why do you only test for success? in the event of failure, you would never
know if the request had failed or why. in addition, there are a number of properties available
in okhttputils2's response object. you might want to check them for any hints as to
what is happening on the response side.

in php, you've enabled error reporting; are you reporting them somewhere? are
errors (if any) being returned to okhttputils2? if not, can you make that happen?
echo "fail" is not helpful. i'm not seeing any display_errors command. are there
any errors? what do you see in okhttputils2's getstring method?

in php, i assume $_FILES['file']['name'] refers to the uploaded file (file=name).
what, then, is $_FILES['file']['tmp_name'] ?

a comment: unless you know for a fact that http and https are handled by the same server,
it is possible that they are not. there is no requirement that a server listening on port
80 be the same as a server listening on port 443.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Test.
Ctrl-B

1701759858750.png
 
Upvote 0

msucho

Member
Licensed User
Longtime User
it's often difficult to guess why something occurs without being able to test using a poster's code and endpoint(s).
but i do have some questions:

in okhttputils2, why do you only test for success? in the event of failure, you would never
know if the request had failed or why. in addition, there are a number of properties available
in okhttputils2's response object. you might want to check them for any hints as to
what is happening on the response side.

in php, you've enabled error reporting; are you reporting them somewhere? are
errors (if any) being returned to okhttputils2? if not, can you make that happen?
echo "fail" is not helpful. i'm not seeing any display_errors command. are there
any errors? what do you see in okhttputils2's getstring method?

in php, i assume $_FILES['file']['name'] refers to the uploaded file (file=name).
what, then, is $_FILES['file']['tmp_name'] ?

a comment: unless you know for a fact that http and https are handled by the same server,
it is possible that they are not. there is no requirement that a server listening on port
80 be the same as a server listening on port 443.
Thanks for respond. Mi answers bellow
I debug the php errrors on the server and always the $_FILES variable is empty on https and fine on http
I add vardump($_FILES) and the result on b4a is :
array(0) {
}
Related to the server only one apache server is working on both ports
 
Upvote 0

GeoT

Active Member
Licensed User
Longtime User
Hi.
Are you using a local server? Is it XAMPP or WAMP?
If not, did you look at the letsencrypt documentation?
 
Last edited:
Upvote 0

GeoT

Active Member
Licensed User
Longtime User
Hi. OK.
Have you already linked your website to a domain in AWS with several DNS and the public IP of your computer?
Did you activate https correctly?
Depending on the operating system of your computer, I think it is activated in one way or another through the console with a series of commands.
 
Last edited:
Upvote 0

msucho

Member
Licensed User
Longtime User
Hi. OK.
Have you already linked your website to a domain in AWS with several DNS and the public IP of your computer?
Did you activate https correctly?
Depending on the operating system of your computer, I think it is activated in one way or another through the console with a series of commands.
Have you already linked your website to a domain in AWS with several DNS and the public IP of your computer?
Yes, all the site works fine on ip public address on https, (web pages, apis on https)

in fact, I am using the same server the PostString and it works perfect but the PostMultipart doesnt work on https.

Code:
Dim mapainicio As Map = CreateMap("USER":"USER","PASSWORD":"PASSWORD")
    Dim j As HttpJob
    Dim URL As String = "https://www.mydoman.com/apis/apimaestra.php/"
    Dim Endpoint As String = "endpoint0"
    Dim Link As String = $"${URL}${Endpoint}"$
    Log(Link)
    Log(mapainicio.As(JSON).ToString)
    Try
        j.Initialize("", Me)
        j.PostString(Link,mapainicio.As(JSON).ToString)
        Wait For (j) JobDone(j As HttpJob)


Thanks for your replay
 
Last edited:
Upvote 0

GeoT

Active Member
Licensed User
Longtime User
Hi.
Did you try with Job.UserName and with Job.Password for the PostMultipart?
Do you use API Gateway?
Do you use AWS Signature?
 
Upvote 0
Top