HTTP post with file - without cURL

karel.laska

New member
Hello, I would like to ask for help with my setup.
I created a webcam where macrodroid took a photo, made a copy of the file in a folder and renamed the file.
This works for me...
Now I would like to send the renamed file using post to a PHP page.
I managed to get this working on a Xiaomi 10 Pro phone, using cURL.
The phone that will eventually be used is an older Samsung that does not support cURL.
I tried to use the built-in HTTP request function but I can't send the file. I don't know how to force the label "file" on the attached file so that it behaves like a form.
In the response, I see that the file has been sent (data size), however, I get the error "error while uploading the file".
I tried browsing upload macro, forum and videos but couldn't find it.
Thanks for any ideas or direction..

Karel
 

RSF

Well-known member
Can you post the cURL setup / parameters that worked? That may help inform how to convert that to a MacroDroid HTTP Request (Post) action.
 

RSF

Well-known member
curl -d -F "file="/storage/emulated/0/DCIM/MacroTemp/Picture.jpg --silent -X POST http://www.adress.com/uploadimage.php
Can you double-check this? I thought curl -d and -F options were mutually-exclusive... Does the PHP server expect the parameter as data or a file?
And are you sending just the name of the file, or the file contents (which would usually be shown as something like
... -F "file=@/storage/emulated..." )

Also, is the PHP site yours? (i.e., could it be changed to accept the file contents as data vs. a form field, which MacroDroid may not support)?
 
Last edited:

Dimlos

Well-known member
I was able to send a file using the file picker with HTTP Request Shortcuts, but it may be difficult to send a file with a text description.
It may be easier to use cURL with Termux.
 

RSF

Well-known member
@Dimlos - I thought of HTTP Request also, and still think it might work well, if a file needs to be sent via that POST call. That app allows for a description (or at least a name) to be attached, assuming the PHP site is looking for it in the way HTTP Shortcuts sends it ...
Screenshot_20240326-205138.png
Termux/cURL is good, too.

@karel.laska - HTTP Shortcuts is a separate app available in the Google Play app store, but which integrates well with MacroDroid as a "Plug-in". MacroDroid can call a "HTTP Shortcut", to do things (like send a file within a POST multipart/form-data request) which MacroDroid doesn't yet do.

Termux, similarly, is a separate app, but one which integrates well with MacroDroid.
 

karel.laska

New member
Can you double-check this? I thought curl -d and -F options were mutually-exclusive... Does the PHP server expect the parameter as data or a file?
And are you sending just the name of the file, or the file contents (which would usually be shown as something like
... -F "file=@/storage/emulated..." )

Also, is the PHP site yours? (i.e., could it be changed to accept the file contents as data vs. a form field, which MacroDroid may not support)?
@Dimlos @RSF
I will try "HTTP Request Shortcuts" thanks for the tip. I hope it will be available for this old phone

@Dimlos Yes you are right :) The correct notation is
curl -f file=@/storage/emulated/0/DCIM/MacroTemp/pic.jpg -X POST 'bastlkoutek.cz/md/upload.php' --connect-timeout 20
I got a line wrong. :confused:

I give including the code with the possibility of trying it...
http://www.bastlkoutek.cz/md/ for preview...
And for interest php... I use same for test POST and GET data...

curl -d variablePost=123 -X POST 'bastlkoutek.cz/md/upload.php' --connect-timeout 20
curl "bastlkoutek.cz/md/upload.php?variableGET=135" --connect-timeout 20

<?php
header('Content-Type: text/plain');

echo "HTTP Content-Type: " . $_SERVER['HTTP_CONTENT_TYPE'] . "\n";
echo "Content-Type: " . $_SERVER['CONTENT_TYPE'] . "\n";
echo "File: " . $_SERVER['HTTP_FILE'] . "\n";
phpinfo(INFO_VARIABLES);

echo "Head:" . '<br>';
foreach (getallheaders() as $name => $value) {
echo $name . ' = ' . $value . "\n";
}
echo "Post:" . "\n";
//Post
foreach ($_POST as $param => $value) {
echo $param . ' = ' . $value . "\n";
}
echo "Get:" . "\n";

foreach ($_GET as $param => $value) {
echo $param . ' = ' . $value . "\n";
}

echo "File:";
// file exist?
if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
// info about file
$fileName = $_FILES['file']['name'];
$filePath = $fileName;
// move file
move_uploaded_file($_FILES['file']['tmp_name'], $filePath);

// answer
echo "File $fileName uploaded.";

} else {
// Pokud došlo k chybě při nahrávání souboru
echo "Error: {$_FILES['file']['error']}";
}
?>
 

RSF

Well-known member
The correct notation is
curl -f file=@/storage/emulated/0/DCIM/MacroTemp/pic.jpg -X POST 'bastlkoutek.cz/md/upload.php' --connect-timeout 20
Ah. Then you'll need something like HTTP Shortcuts or Termux/cURL; I don't think MacroDroid supports sending a multipart/form-data POST request containing a file, yet. According to Google Play, HTTP Shortcuts works on Android 5.0 and higher. Hopefully that'll work for your phone, if it can't run (or doesn't have) cURL.
 

Dimlos

Well-known member
The image could be sent if encoded in base64, although it needs to be decoded on the PHP side.
It was possible with MacroDroid, but if the size of the image is large, the character limit of the variable may be a problem.
 

Attachments

  • Macro.jpg
    Macro.jpg
    562.3 KB · Views: 18
  • ContentBody.jpg
    ContentBody.jpg
    312.9 KB · Views: 18
  • Like
Reactions: RSF

Dimlos

Well-known member
The PHP test code looks like this.
Code:
<?php
if($_FILES['file']){
  move_uploaded_file($_FILES['file']['tmp_name'], 'C:/PHP/img/post.png');
  $img = file_get_contents('C:/PHP/img/post.png');
  file_put_contents('C:/PHP/img/post.png', base64_decode($img));
}
?>
 
Top