0

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()
}
Community
  • 1
  • 1
AodhanOL
  • 630
  • 7
  • 26
  • Have you tried posting the form to `http://ADDRESS/cgi-bin/R/textareascanner`? – hrbrmstr Sep 25 '14 at 12:08
  • I had actually just been trying that out after posting this - I can definitely post data to it, now I'm just having trouble posting the data from my file. I've tried curl -X POST -v --form MyText=@C:\DATA\tester.txt http://ADDRESS/cgi-bin/R/textTest but it complains about parsing it. – AodhanOL Sep 25 '14 at 12:17
  • I think the issue is that if I send it directly, request$c.type won't exist...any advice? – AodhanOL Sep 25 '14 at 12:21
  • 1
    Try adding `-H "Content-Type: application/x-www-form-urlencoded"` which will explicitly set that header. – hrbrmstr Sep 25 '14 at 12:55
  • Thanks - I actually solved this another way, by passing the data directly, but when I'm implementing it fully I'll check out your solution too. – AodhanOL Sep 25 '14 at 13:33
  • Also, the HTML form is expecting the data to be encoded as `multipart/form-data` not `application/x-www-form-urlencoded`. These two representations are not interchangeable. – Andrew Lambert Sep 25 '14 at 20:34

0 Answers0