HTTP POST

  • Board Nominations
    Nominations have now closed and the results are available here.
  • Hey Guest, MARCHintosh 2026 is upon us. Check out community projects, join GlobalTalk, and have fun!

cc333

Tinkerer
Dec 22, 2021
43
26
18
California, USA
I have an audio streaming project, and I'd like to have my playlist automation and playback software update a text file named 'nowplaying.txt' via the HTTP POST command so that a basic web page I created can parse that file (I haven't figured out how yet) and display it on the web page so the user can see what title is currently being played.

Anyway, it seems like everything's in place that should be in place, and it superficially *appears* to be working, yet, no matter how many times the software sends POSTs, nothing happens to the text file on the server. It's as if the data goes into some sort of black hole instead of to its intended destination.

Anyone have any ideas on what's going on and how I might fix it?

The server is Apache2 running on Debian 13 (I think).

c
 

eric

Administrator
Staff member
Sep 2, 2021
1,218
2,099
113
MN
bluescsi.com
There's a lot of moving parts there - do you have the exact command (try to reproduce it in curl, use -vv to get very verbose logs) - what is running behind apache that writes the file? What do the logs in apache say?
 

cc333

Tinkerer
Dec 22, 2021
43
26
18
California, USA
I think you want PUT and not POST, unless you have something on the other end (CGI, etc.) that is actually replacing the file.
I don't have anything set up at the other end (what is CGI other than Computer Generated Imagery?? EDIT: Ah, some quick research tells me it means Common Gateway Interface) because I figured it was automatic, but perhaps it's not?

I'd love to use PUT, but the software I'm using only supports POST and GET. I'm not sure GET will do what I want, however, so POST would seem to me to be the only viable option.

c
 

ClassicHasClass

Active Tinkerer
Aug 30, 2022
455
271
63
www.floodgap.com
POST says to the server, "I am sending this data, which I want you to use to change something, according to the resource I am sending it to." POST implies a change in state may be made on the server's side, so it is not necessarily safe to send the same POST data multiple times (i.e., it is not necessarily idempotent). The server gets to decide how to act on that data, which may mean practically anything. Since it isn't defined what the behaviour is, you need some program on the other end to receive the data and (in this case) put it in the file.

PUT says to the server, "I am sending this data, which I want stored at the location I am providing." PUT speciifcally says it wants the resource named changed to match the data you are sending. If you send it multiple times, the result is the same, so it is idempotent. You can configure some servers to do this for you automatically, or with authentication.

You don't want GET for this. GET says to the server, "I am sending this request, which I want you to use to do something, but not change anything." GET implies no change of state is made, so it is idempotent. This has certainly been abused in the past for things for state-changing tasks, but at any rate, GET wouldn't be appropriate for this.

In your case, you basically need POST to act like PUT. Since POST is generic and implies nothing about what the server will actually do with the data, that means you'll have to write something on the server side. You have Apache, so a simple CGI in Python or Perl may be the easiest approach. Just make sure you sanitize your inputs well, since anything writing files to disk can always be abused if it is improperly secured.

See also https://www.reddit.com/r/apache/comments/1c3z0fi
 

ClassicHasClass

Active Tinkerer
Aug 30, 2022
455
271
63
www.floodgap.com
That should work. I'm assuming you are writing to a specific, hardcoded file path with the PHP rather than taking the filename provided by the client, which I would strongly advise against.

If this is a publicly accessible URL, you may wish to secure it somehow - which could be giving the URL out only to people who need to use it (you?), though bear in mind that makes the URL basically the password.

Glad it's doing the job for you.
 

cc333

Tinkerer
Dec 22, 2021
43
26
18
California, USA
That should work. I'm assuming you are writing to a specific, hardcoded file path with the PHP rather than taking the filename provided by the client, which I would strongly advise against.
Yes, the name is hardcoded into the script, and the software POSTs the data, such as it is, to the PHP file.

If this is a publicly accessible URL, you may wish to secure it somehow - which could be giving the URL out only to people who need to use it (you?), though bear in mind that makes the URL basically the password.
It is, but I can probably change it to an internal address.

Glad it's doing the job for you.
Me too!

c
 

cc333

Tinkerer
Dec 22, 2021
43
26
18
California, USA
OK, I had that formatting problem where spaces became "+", "&" became %3F, etc.

Obviously there's some kind of text encoding problem, but I'm too much of a novice to figure it out right now. I'm lucky I got where I did! And I didn't use any AI to do it!!

Anyway, what I did instead was hack together some PHP that iterated over the file a few times using str_ireplace() to replace the various incorrect characters.

It works and gets the job done, but it's definitely not the most elegant thing you'll ever see:
PHP:
<?php
$putdata = fopen("php://input", "r");
$fp = fopen("nowplaying.txt", "w");
while ($data = fread($putdata, 1024))
    fwrite($fp, $data);
fclose($fp);
fclose($putdata);

$myFile = fopen("nowplaying.txt", "r") or die("Can't Open file");
$one = fread($myFile, filesize("nowplaying.txt"));
$two = str_ireplace("+"," ",$one);
$three = str_ireplace("%27","'",$two);
$four = str_ireplace("%22","\"",$three);
$five = str_ireplace("%3f","?",$four);
$six = str_ireplace("%26","&",$five);
fclose($myFile);

$fixed = fopen("nowplaying.txt", "w");
fwrite($fixed,$six);
fclose($fixed);
?>
If you have any suggestions as to how I could fix it up, I'd appreciate it!

c