HTTP request, Trigger and Action

Post your questions and help other users.

Moderator: Martin

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

HTTP request, Trigger and Action

Post by Desmanto » 30 Jul 2017 06:48

Automagic has HTTP request both in Trigger and Action. But they do different things. Trigger HTTP request will start a lightweight web server to be triggered from somewhere else. While Action one is the one which request to other server, can be internet, local web server or other phone with automagic trigger http request enabled.
Trigger : server
Action : client

I have been tinkering with the Action HTTP request : POST, to transfer variable from the client to the server, example to set the clipboard to the server or vice versa. Or using get GET method to retrieve file at the server. Now the problem is the way to upload file from client to server using the PUT method.

First I initialize the file I want to upload using Action Init variable Text File, save in variable file_text. Then use action HTTP request on client, PUT method, with content type set to General Test (text/plain) and the Data is variable file_text. When I execute it, the file (variable file_text) was sent properly. But in the server it was save into variable file_content = /storage/emulated/0/Automagic/httpd/upload-1178410505.tmp. If I check that file, it has the same content as the file from the client. Which means the upload was successful, but it was save with some kind of random name and the path to the file was saved to file_content.

So the question is how to use the variable file_* in the HTTP request trigger help? I can actually rename the file and move it into the proper folder, but I think there must better way to do this, using the provided file_* variable.
The reason I wanna do this is to completely ditch off autoremote. Clipboard back-thru is done, sending command is done, file download is done, only file upload left to complete it.

I have tried using tasker action : HTTP post (there is no HTTP put). There is a row to choose the file (to be uploaded). But when i execute it, the is nothing appear on automagic server. So there must be some protocol missing or maybe it doesn't work with post method.

===================
Additional question is about how the trigger HTTP request work. Sometimes it listen not only to wifi interface (local wifi IP), but it listen to my modem private IP too. Is there a way to limit the interface or even implement a lightweight firewall IP filtering? Just don't wanna leave any open port at the router for maximum security. I know it can be done using the condition to check the Incoming IP, but I think firewall at the trigger side (server) must have higher perfomance, as it can denied the connection and don't trigger the flow.

Thanks in Advance.
Desmanto
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: HTTP request, Trigger and Action

Post by Martin » 30 Jul 2017 19:03

Hi,

You could directly read the file on server using an action like Init Variable Text File: {file_content} and store the content of the upload in a variable for further processing. Another method would be to rename the file in the flow.

Probably all requests that will reach your phone on the defined port will be passed to Automagic. I never had a chance to test this since all of our providers here seem to use NAT for the phones so actually sending something to the phone is quite difficult. Adding a condition is probably the way to go for now. I could add a checkbox to bind the socket only to the WiFi interface which should fix this problem.

Regards,
Martin

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: HTTP request, Trigger and Action

Post by Desmanto » 31 Jul 2017 12:58

Thanks for the reply. And that will be nice to limit the interface for future release.

That means i still need to manually rename it. Init file then write file also works. Now the next problem, Http put transfer the file stream, but not the file name. So I can't get the name in single trigger. If not done in single trigger, that means I should have extra GloVar to store the temporary data. If I can't get it using Put, then now I tried to use HTTP post with content type General Text (text/plain). Because Put will save unnecessary file at the /automagic/httpd/. While post only save it to variable. Using Post with General Text Content type, Data :
filename={file_text}

The result at the server, there is a param_filename and the content is exactly as the file transferred, but blank lines and the end of file have been truncated. I can extract the filename using getallvariable(), and parse it using regex. However, because filename can contain special character, when using such as "hello.txt", automagic ignore this param, file not transfered.

I google for some time, there is a possibility to upload the file, but Content Type must be multipart/form-data. I tried to change the text/plain to it, but it didn't trigger. So can't use that. Maybe you can expand the option of the http request to support more content type.

Last resort, i just put the filename directly at the requested path, need to encodeURL first. Then at server, substring the requested path to take out only the file name. This will transfer the file and the file name as well in single request.
However, this init text has limitation of 1 MB. I wanna upload various of files, mostly under 100 MB. (example I can transfer picture/video/text/zip or anything and have it open directly at my phone). Try to use init image, it only works for image, not for other filetype.

The other workaround which is kinda ugly, is to http post only the path to the file (from client). Client will then start its own server. The real server will then use the path to download the file from the client. (because download works properly, but upload didn't). Is there any special way to upload the file thru the http request? Or do I miss something?
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: HTTP request, Trigger and Action

Post by Martin » 01 Aug 2017 20:03

The entire HTTP Request is mostly meant to be used for simple relatively short data at the moment. Transferring large files probably needs to be implemented with some entirely new options in the action.. Maybe HTTP Put could allow to specify a file that will also properly handle binary data. You could transfer the filename also by using a custom header.

Maybe you could resort to transfer files using an entirely different mechanism like SSH or FTP or use Dropbox/Google Drive to upload the file temporarily to the cloud (if this is an option at all).

Regards,
Martin

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: HTTP request, Trigger and Action

Post by Desmanto » 02 Aug 2017 05:39

Actually just wanna remove the autoremote plugin. Automagic webserver is more realiable and simple than autoremote. The server can be always on as long as the flow is enable, while autoremote can sometimes break when the wifi signal drop. The only missing things is this file upload only. So maybe this http post/put upload can be integrated in the next update. And probably can add https to the server also if possible (optional).

I have ftp server, but it add additional latency when starting the server. And ftp server tends to be unreliable on android, probably randomly get killed by the system manager, even I have whitelist it. I use it for transfering big files, and sometimes the handler just error, and don't delete file properly after moving.

After googling and testing for a while, it turns out I can use cURL to upload the file, use filename as parameter. The file name and the file content transfered, no init variable needed, so no size limit or filetype limit. I just need to specified the path. The server also return variable file_filename, and variable param_filename which contain the real file name. But it is still saved in /sdcard/Automagic/httpd/upload-1591495723.tmp (random number). Just need to rename the file to real file name, and done. 80% completed, only need to implement it and finally I can ditch off autoremote too. Yay. :)

Thanks for the help.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: HTTP request, Trigger and Action

Post by Martin » 03 Aug 2017 19:20

Could you please send an example curl command that works for you? Adding file upload to the HTTP Request is something I have on my todo list but I would like to make sure that the method you use with curl can also be done in action HTTP Request.

Thanks,
Martin

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: HTTP request, Trigger and Action

Post by Desmanto » 04 Aug 2017 05:06

I download the Curl for Linux - Android, 7.40. Extract only the curl binary, put it into /storage/emulated/0/Automagic/Resources/curl
Create a simple text file, named a.txt, type something and save it at root folder of /sdcard.

Then I create Action : Execute Command
Command : curl -F 'data=@/storage/emulated/0/a.txt' http://192.168.1.100:8080/automagic/file/tes
Working Directory : /storage/emulated/0/Automagic/Resources
Other field just leave default.

Prepare the trigger for HTTP request (Experimental), enable the flow
Path : /automagic/file/*
Port : 8080
Keep WiFi awake : checked

I assigned a debug dialog for the trigger, just to check for the available variable and result of the upload. Execute only the action command. The debug dialog show me the variables, some interesting ones are :

file_data : /storage/emulated/0/Automagic/httpd/upload930509040.tmp (this can be rename using {param_data})
header_content_type : multipart/form-data;boundary=----------------------------39beec3ff9e5
method : POST
param_data : a.txt (this is the file name, has been parsed automagically)
request_path : /automagic/file/tes ("tes" can be used to modify various command, such as whether to open the file directly, just rename or maybe directly share to other app)
trigger : HTTP Request: /automagic/file/*, port 8080

Then I browse to /storage/emulated/0/Automagic/httpd/upload930509040.tmp. The content is exactly the same, md5 checksum the same. (previously using HTTP Request PUT, the blank line at the end of text file get truncated).

Automagic side is done, upload to phone can be done. Now I am confused with the eventghost side. Tried the same upload with curl, eventghost webserver react in different way. But of course that is eventghost's problem, gotta sort it out as well.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: HTTP request, Trigger and Action

Post by Martin » 05 Aug 2017 19:00

Thanks for the explanation.

The next EAP build will have a new feature to upload the file with action HTTP Request. You will be able to change the field Content Type to Form Data File Upload and select the field name and the file to upload.
Let me know if it works for you once the new EAP build is released.

Regards,
Martin

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: HTTP request, Trigger and Action

Post by Desmanto » 06 Aug 2017 14:38

Wow, that will be nice. Can't wait to see it.

After using Automagic for about 2 months, just realize the script is really easy to use and debug.
I can parse quite a complex text in automagic, but can't do the same yet in eventghost :lol:
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

hmast
Posts: 26
Joined: 17 Jan 2019 14:50

Re: HTTP request, Trigger and Action

Post by hmast » 18 Feb 2019 00:42

while autoremote can sometimes break when the wifi signal drop
For some who are not aware and not wanting to make simple things complicated and to make sure things come through with autoremote, could set up feedback loop to confirm sending. If not, send again. Then if not again still, send message, "autoremote was unable to send x the second time". So if it sometimes won't go through, will send again, until it gets thru or giving a note that "hey, sent multiple times and the damn thing didn't want to go thru."
Possible=ISeeAWay, Impossible=IDontSeeNow, ImpossibleButImportant=WeNeedToFindAWay

Post Reply