Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Save data to url with http.request

edited October 2012 in General Posts: 100

In the refference I read that there are parameters to use if http.request is used to set data to a url file instead of retreving them.
The parameter "PUT" or "POST" must be for sending but there are no further explanation.
Does anybody have an example to read and learn from ?

This is the absolute last bit of my first Codea App, so thanks in advance B-)

Re Peter

Comments

  • I have now read almost any post in this forum and a lot about Apache servers.
    But I cannot get it to work.....Please help.

    My simple test code looks like this and the goal is to write to a file on an Apache server set up on a Macbook running Mountain Lion.

    function setup()
        tbl={["method"]="POST",["data"]="testing"}
        http.request("http://192.168.1.141/clicXdataChanges",getHttpData,httpError,tbl)
    end
    
    function getHttpData(data,status,headers)
        print(data)
    end
    
    function httpError(error)
        print("Error:"..error)
    end
    

    It reads the file but should write "testing" to it !
    All permissions should be in order on the server side for the specific file and it is writable from the computer.

    Re Peter (getting grey haired quickly now)

  • Posts: 1,495

    Hi @macflyerdk,

    I need to learn how to do this. I assume you've seen all the other posts on it?

    Couple of guesses - first, the Apache server address (locally) is 127.0.0.1:8080. But that's only if your server is on your source machine and using port 8080.

    • second, your getHttpData() function doesn't have any parameters in with the line within setup - should it read ......., getHttpData(dat, stat, headr), ....

    Not literally, dat, stat and headr would have to be defined.

    Or perhaps you need to rename your getHttpData() function?

    Hope that helps.

    Bri_G

    :)

  • edited October 2012 Posts: 580

    .@Bri_G, no, he's got that part right. getHttpData is his callback function that gets called when the request succeeds. The request function will call getHttpRequest and pass the params to it. Sorry, can't really be much help here otherwise, I've not tried to do something like this yet.

  • It does function, but only reading, not writing.
    And this is where I do not get it.
    Something is apperantly wrong, but I haven't come accross any example that writes to a file, so I have nothing to compare with.
    Tried renaming my function, but same result.

  • Posts: 580

    Well, I don't know much about this, but it seems to me that you need some sort of program on your server that accepts requests, and does something with the data you send it. So right now you are sending a 'POST' to http://192.168.1.141/clicXdataChanges with "testing", but how does your server know what to do with that information? Figuring that might might lead you to your solution.

  • Posts: 1,495

    Hi @toadkick,

    Thanks for the feedback.

    Looking at the reference this call is set up for the get function. So it can read the data.

    Looks like we need some kind of Put, Post or Mail functionality to either write a file to the website or post data to a php file which then modifies the website directly.

    Can't see that in the reference, but then again I've only seen Http transfers using HTML code.

    Further thought, the call used above is the most advanced and expects a parameter list in the table of the call.

    I can confirm your code works as I read the index.html file on my website.

    Keeping an eye on this thread as I need to learn how to do this. Good luck with it.

    Bri_G

    :)

  • edited October 2012 Posts: 580

    Well, you probably won't find much about this in the Codea reference, since this isn't really a Codea issue. Like I said, you need something on the server end to recognize the request you're sending, and know what to do with it. I've not really had any experience with server side programming, but I've done a lot of work on the client side with other guys who are server programmers. Typically I would send a 'POST' request to a specific server address, like http://someserver.com/somepage or whatever, with some data, and the server side software (I think they use CakePHP) processes the request, does something, and if necessary returns data back to me.

  • Posts: 1,495

    Hi @toadkick,

    Yeah, that's my understanding. I've used php on a website to access a database (eg hi-score table) and then update it, if neceary, with new results and rankings.

    But the key is - you need an HTTP 'POST' functionality. My understanding is Http.request in Codea is the HTTP 'GET' functionality and will download any file you request but not upload it (unless I've read the reference section for Codea wrongly).

    In other words - we can't update a table on the website unless we post a message to the owner with the details - messy.

    Bri_G

    :)

  • edited October 2012 Posts: 580

    .@Bri_G Codea does also support 'POST', as well as 'HEAD, 'PUT', and 'DELETE. Check out the "Parameters" section at http://twolivesleft.com/Codea/Reference/#detail/index/http.get . All you have to do is use the "advanced" version of the call, where you pass a parameter table as the last argument to http.request() with the extra parameters (one of which is the HTTP method to use for the request), exactly the way @macflyerdk did in the code he posted above). The function was originally named http.get (it still is actually), which admittedly could give the impression that only 'GET' is supported, but later TLL provided http.request, which is just another alias for the same function.

  • BortelsBortels Mod
    edited October 2012 Posts: 1,557

    You don't need PUT or POST to write - that's a convention, but it's only necessary if you're trying to make "REST"ful websites and such. What you need is appropriate server-side code.

    So - here's a simple example. Note that it does NOT do many things you'd want to do in the real world, like preventing someone from spoofing it, but it should be good enough to explore the concept. This should run in any semi-modern perl - if you're on windows, you'll want to change the "dbmopen" line to reflect your drive setup. If you have an older perl, the CGI module won't be built-in - go get a new perl, dude.


    #!/usr/bin/perl use CGI qw(:standard); $name = param("name"); $name =~ s/[^\w]//g; $score = param("score"); $score =~ s/[^\d]//g; dbmopen(%SCORE, '/var/tmp/scores', 0666); if (($name eq '') and ($score eq '')) { # no name or score, return list print "Content-type: text/plain\n\n"; foreach (sort keys %SCORE) { print "$_ = $SCORE{$_}\n"; } dbmclose(%SCORE); exit; } $SCORE{$name} = $score; dbmclose(%SCORE); print "Content-type: text/plain\n\n$name = $score\n"; exit;

    For messing around with - I have this up and running at http://home.bortels.us/hiscore.cgi (and will until it gets abused) - go ahead and hit that link for a list of high scores. You can add to them by adding parameters to your http call: http://home.bortels.us/hiscore.cgi?name=Fred&score=17 would give "Fred" a score of "17".

    It's up to your Codea script to parse and display the results, of course.

    In a "real" implementation, you'd want to figure out some way to try to prevent spoofing (good luck with that, really), and to have the server do a "top scores" list for you rather than downloading them all - perhaps have it sort as well. You might want to make it support multiple games as well (I had that, for a bit, but it complicated things) That's left as an exercise to the reader.

    One bit of advice - see the "=~" sections at the top, where name and score are assigned? Those strip out non-word (for name) and non-number (for score) entries. If you don't have that sort of thing, you open yourself up to ugly cross-site scripting attacks. Be VERY careful messing with server side stuff - it's easy to open yourself wide open to various attacks, especially if you re-display the data sent. Also be aware that if you get popular, the simple perl cgi above could (would!) easily crumple under real load. Fair warning.

  • Starting this thread I was hoping for an answer like the one you came up with Bortels, so thanks very much :)
    But being a total beginner in Perl, I have spend most part of today trying to pass a parameter to my server Perl script. Not a succes I may add.
    My Apache server is running locally on my network on a Macbook with Mountain Lion.
    Server is tarted and will run the Perl script, which should write the parameter to a txt file.
    Writing to a file from within the script works using a simple string, but it should be the parameter instead.
    I have tried in and out of reading the parameter, but ends up with a blank variable everytime.
    I hope this makes some sence to somebody who knows Perl.

    I even ended up bying Phytonista so I could use the direct method via UDP which is missing in Codea. But Pythonista seems a bit more complicated than expcted.

    Re Peter

  • BortelsBortels Mod
    Posts: 1,557

    I messed with pythonista trying to make udp work - and failed. One of the problems there (of MANY) is that the docs are stock python - and so talk about things that flat out won't work on the ipad. And without a forum, there's no good way to get help. I am told a forum is coming... someday. (That from an email I sent to the developer).

    So - apache on a mac should run the above without change. If it's short - post your code, maybe I can figure it out. Did the example above - unmodified - work for you? That's a direct cut/paste from the version I have up right now on my home website. I'd start with something simple, just to confirm the CGI is getting called correctly:


    $!/usr/bin/perl print "Content-type: text/html\n\n"; print "<H1>Hello World!</H1>The current time is " . scalar(localtime());

    Call that "hello.cgi", do a "chmod a+x hello.cgi" to make it executable, and put it in your documentroot (the systems settings -> sharing should show you the path).

  • Finally......Got it working :)
    Spending so much time reading about Apache servers and stuff combined with hours of try and error, I finally managed to run the server properly using MAMP combined with php script and passing arguments to a txt file.
    So now I have have io connection between Codea and my Macbook.
    Just for clarifying. There are no need for POST PUT etc. In the http.request, it just goes directly.
    I should make a list to my approach, but fear I cannot remember anymore what actually happend.
    Thanks for leading me in the proper direction (the server side needed to be properly configurated)
    Re Peter

  • Posts: 1,495

    Hi @macflyerdk,

    I've just set up a remote server to build a hi-score table on, and am currently looking at the php code needed. I'm going to run it via an HTML page initiating the call first then sync it with Codea.

    Since it's a web page - to view it you have to use a browser, so - question - is there any way of initiating a browser from Codea and passing on the hi-score parameters with the call??

    I'll let you know how I get on.

    Thanks,

    Bri_G

    :)

  • Posts: 666

    Any thoughts on modifying this to upload (POST) to a dropbox folder?

  • BortelsBortels Mod
    Posts: 1,557

    Dropbox has a developer's API you could write to that should work fine with Codea's http support: https://www.dropbox.com/developers/reference/api

  • edited October 2012 Posts: 100

    @Bri_G,

    With a php file/web page it is quite simple to pass parameters from Codea to ex. a txt-file on your computer.

    The call from Codea is:

    http.request("http://192.168.1.141:8888/yourphpscript.php?argmt="..123test, dataAvailable)

    and these lines in yourphpscript.php in your full access server folder:

    <html>
    <head>
    <title>PHP Write an argument to a txt file</title>
     </head>
     <body>
     <?php $fh=fopen("dataFromCodea.txt","w"); ?>
     <?php fwrite($fh, $_GET["argmt"]); ?>
     <?php fclose($fh); ?>
     </body>
    </html>
    

    No more to it if your local server works and accepts php.
    If the file do not exist it is created.
    You need of course to substitute the server address with you own.

    Re Peter

  • edited October 2012 Posts: 666

    @Bortels I do already have JSON parsers and some basic knowledge of the http.request in Codea. I'm wondering how many round trips I'll have to do with DropBox to get this running...

  • BortelsBortels Mod
    Posts: 1,557

    Dunno - I have yet to bite the bullet and get a developer ID for dropbox. It doesn't look too horrible - and if you already are JSON-ready, I don't expect any showstoppers. Good Luck!

  • edited October 2012 Posts: 666

    Well, with OAuth needing SSL, I'm out of luck with posting to Dropbox. Too bad...

  • BortelsBortels Mod
    Posts: 1,557

    I'm looking right now into using Amazon Web Services DynamoDB as a way to do this - it's a NoSQL database with a RESTful interface (meaning you can talk to it with the Codea http.get stuff) - and it looks like for most hobby usage it will fall into the "Free" category. The one thing I can't figure out is how to make it do a high score list - and I haven't dug into it much.

  • Posts: 666

    Update: I have basic LUA-coded dropbox integration enabled with App folders (didn't try PUBLIC ones yet). Once I simplify the code (and back it up to DropBox vs. GIT) I'll post it.

  • Posts: 666

    @Simeon, Here's an inconvienence - when loading a JPG via HTTP (and probably any image type), Codea converts it to an image(). The problem - I want to read the RAW JPG/GIF/PNG and copy it, but since I can't seem to get to the RAW data, I can't do a copy.

    Is there some override that allows me to get the unconverted HTTP result for something like a JPG so I can do a copy?

Sign In or Register to comment.