My problem is as follows. I have a website with a very simple form that looks like this:
'Enter text in a table.'
'Hit submit - data is run through a simple RScript and output to the screen from R in a different webpage.'
This works fine if I input the data manually to the website, but I want to be able to do this programmatically using curl. The code for the pages is at the bottom of the question.
I have a feeling that this question might help, but I can't see how to link it back to what I want to do... Posting data to web forms using submit buttons from R but I don't want to do it using R if it can be avoided.
Ultimately I would like to be able to retrieve the data from a file to do this, but I'd be happy if I could get it working with text I input from CURL.
Here are the attempts I've made and the output from them (as far as I can tell from this, the data is uploaded correctly but not stored in the form to be passed to textareascanner). Should I be pointing to the form when I enter the URL somehow? Or using a different http method? Thanks for any help.
Attempt using data input from command line:
> curl -X POST -v --data "MyText=123T&press=OK" http://ADDRESS/cgi-bin/R/mdmSubmit
> * upload completely sent off: 21 out of 21 bytes
> Error in (function () : unused arguments (MyText = "123", press = "submit")
My attempt with reading the data from a file:
curl -X POST -v --form MyText=@C:\DATA\test.txt --form press=OK http://ADDRESS/cgi-bin/R/mdmFileSubmit
> POST /cgi-bin/R/mdmFileSubmit HTTP/1.1
> Content-Length: 293
Error in (function () :
unused arguments (MyText = list(filename = "tester.txt", tempfile = "/tmp/Rtmp
4pk5LV/Multipart584b104386fb", content_type = "text/plain", head = "Content-Disp
osition: form-data; name=\"MyText\"; filename=\"tester.txt\"\r\nContent-Type: te
xt/plain"), press = "OK")
FastRWeb script:
run <- function() {
out("<!doctype html><html lang='en'><head>",
"<meta charset='UTF-8'/></head><body>",
sep = "", eol = "")
out("<form method='post' action='textareascanner' ",
"enctype='multipart/form-data' name='form'>", sep = "", eol = "")
out("<textarea name='MyText' rows=15 cols=80></textarea>")
out("<br/><input type='submit' name='press' value='OK' /></form>")
out("</body></html>")
done()
}
And the page it redirects to:
run <- function(MyText) {
# HTML
out("<!doctype html><html lang='en'><head>",
"<meta charset='UTF-8'/></head><body>", sep = "", eol = "")
# Retrieve the data in a 'multipart/form-data' form
if (grep("^multipart", request$c.type)) {
pars <- parse.multipart()
}
# Scan the text
try(fullText <- scan(textConnection(pars$MyText),character(0),sep="\n"))
# HTML output
if (exists("fullText")) {
oprint(fullText)
}
out("</body></html>")
done()
}