Using an output file stored on the OpenCPU server as input to a subsequent function call - opencpu

Question: how can I use an output file stored on the OpenCPU server as input to another function?
Background:
I am attempting to use knitr and markdown within openCPU to generate html that I can use to update a webpage with statistical information on the page load.
The basic workflow is as follows:
Generate an .Rmd file, store locally.
Access a webpage that uses AJAX to upload the .Rmd file to an OpenCPU instance on the server.
Use the knit function via openCPU to turn the function into a *.md file stored on the server.
Use the markdownToHTML function on the file stored on the server (by passing in the appropriate hash generated via the call to knit) and receive an AJAX reply that contains the generated HTML.
Update web page with new HTML.
As it stands, I have this process working up to step 4. I can call knit passing in an .Rmd file via a form request POST, and I receive the following reply from OpenCPU:
{
"object" : "xa9eaea44e1",
"graphs" : [
"xf31dcfe7f3"
],
"files" : {
"figure" : "xfc55396fd8",
"test.md" : "x7821c69f79"
}
}
where "test.md" is the output file generated via the knit function. Now, I attempt to use the hash (in this case "x7821c69f79" by POSTing to /R/pub/markdown/markdownToHTML/ascii with the following parameters:
file /R/tmp/x7821c69f79/bin
This returns an HTTP 400 error with the following message:
cannot open URL 'http://localhost/R/store/R/tmp/x7821c69f79/bin/rds'
However, when I make a GET request to /R/tmp/x7821c69f79/bin, the contents of test.md are returned. So I know the file is being stored correctly on the call to knit.
So, what's going on here? In other words, how can I use an output file stored on the OpenCPU server as input to another function?

Hmz the /store error looks like a bug, I'll look into that.
Maybe in step 3 you can have the function return the contents of test.md, e.g. end with return(readLines(test.md))? Or better yet, don't output to test.md but to a tmpfile() and return the contents of that. This way the output is stored as an R object in the store, rather than a raw file, and you can just pass an argument e.g. file=x7821c69f79 in step 4.
Did you have a look at the markdown example app? See source here and here.

Related

JMETER: Need to send all entries in a CSV file to HTTP request body in 'one' request

I'm trying to solve a test data issue in Jmeter. Can anyone of you have a look at below problem statement and advise here please ?
Requirement: Need to send all entries in a CSV file to HTTP request body in 'one' request to the end point.
Example CSV File:
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d"
HTTP Request Body:
["${data}"}]
When the data is substituted, I should be able to get below output.
[
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d"
]
Problem Statement: When I use CSV data set config. file, I'm unable to concatenate all entries into one single request body. My understanding is, CSV data set config. the file is not the right option here.
Did some search in StackOverflow and followed a method to achieve above using JSR223 PreProcessor' and the link is, How to send multiple json body using jmeter?.
Followed the above link and tried added below custom code provided.
def builder = new StringBuilder()
new File('/path/to/plans.csv').readLines().each { line ->
builder.append(new File(line).text).append(System.getProperty('line.separator'))
}
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', builder.toString(), '')
sampler.setPostBodyRaw(true)
Upon running, I get below error message,
Caused by: java.io.FileNotFoundException,
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d" (The filename, directory name, or volume label syntax is incorrect)
If the file is not found, then how come the entries are read and displayed in the log viewer?
Also, how do I link the output of custom code to the request body? Or is it taken care of by the custom code itself?
You have an error in your code, change this line:
builder.append(new File(line).text).append(System.getProperty('line.separator'))
to this one:
builder.append(line).append(System.getProperty('line.separator'))
If you want to send the full contents of the file you don't even need to go for scripting, you can use __FileToString() function right in the "Body data" tab of the HTTP Request sampler:
${__FileToString(/path/to/plans.csv,,)}
And last but not the least if you need to generate JSON from the plain text it might be a better idea to go for JsonBuilder class, see Apache Groovy - Why and How You Should Use It and Apache Groovy - Parsing and producing JSON
Two steps:
Add a User Parameter Pre-processor before the HTTP request:
Name: DATA
User_1: ${__FileToString(/path/to/plans.csv,,)}
Add the following to request body:
${DATA}

JMeter not attaching contents of binary file to POST data in HTTP Request

I'm attempting to simulate a login call with JMeter 2.11 to a service that uses a binary format. I've created an Http Request with the appropriate settings, except for the body data. I need to POST raw binary data.
According to the docs here, I should be able to set the file path for exactly one file, with no parameter name, and no other content in the Body Data, and have it place the data in the request body.
If it is a POST or PUT or PATCH request and there is a single file whose 'Parameter name' attribute (below) is omitted, then the file is sent as the entire body of the request, i.e. no wrappers are added. This allows arbitrary bodies to be sent. This functionality is present for POST requests after version 2.2, and also for PUT requests after version 2.3.
However, when I run the test, the POST Data is empty.
I have tried the extra set of plugins for JMeter as well, but alas, I'm stuck. The loaded file has 145 bytes of data, and the request shows that the content-length is 0. What am I missing?
The Http Request
The result
Update 1
To clarify, I am NOT attempting to send a file, I'm attempting to send a binary encoded message as raw POST data.
Switch back to Parameters tab not Post body.
See:
http://jmeter.apache.org/usermanual/component_reference.html#HTTP_Request
Yoy could try recoring to see how the resuest look like.
This is my solution,maybe not best, but it works fine:
1st step :
You should write your binary data to a file (assume it's name is
FILENAME);
2nd step :
For your http request sampler,Yout should put ${FILENAME} under file
path in the "Send Files with the request" section (while leaving its
paramter name empty and specifying an encoding (for binary, it is
application/binary)).
Hope it helps.
Refer to this article

How to get all posted files at once using fineUploader?

I have a fineuploader that allows multiple files to be uploaded in MVC web app.
Example:
Suppose I upload 3 files 5MB, 20MB, 1MB
I expect 3 files to arrive in the controller in single call but they are not.
I want to iterate all 3 files (array) and process
Question 1: Is there a way to get all 3 files at once in the server?
[HttpPost]
public FineUploaderResult UploadFile(FineUpload upload)
{
//upload contains only one file at a time
//for second file it gets call again even though I submit only once
}
No. Fine Uploader sends each file in a separate request. This is not currently configurable. If you need to tie your files together, you can easily do that by passing common parameters with each file via the params option, or the setParams API method. Sever-side, you can look for this common parameter in each upload POST request and deal with the files accordingly.

Using Cake w/ url extensions

I am using CakePHP 2.0 (I believe it is v2.0.3.) and PHP 5.3.8.
I am working on an application which utilizes Cake's support for url extensions. Specifically, I am outputing XML whenever a url request ends in the .xml extension. If the url request is made without any extension, then my application presents the stardard view. This all works beautifully --
request .../controller/action.xml renders via view/controller/xml/action.ctp while request .../controller/action renders via view/controller/action.ctp.
To achieve this, I did the following:
1. Added support for url extension; added the following line to route.php -- Router::parseExtensions('xml');
2. Added support request handling; added the following line to MyController.php -- public $components = array('Session', 'RequestHandler');
To output xml, I am using Cake's 'XML' class in conjunction with PHP's 'SimpleXMLElement' class. My problem is that the complete xml is never generated. The classes are suppose to generate xml based on an input PHP array, however, it appears that the complete array is not processed. My xml output is partial.
My source code in my view file (.ctp) is as follows:
$simple_xml_elem = Xml::build($xml_array);
echo $simple_xml_elem->asXML();
Interestingly, in the course of trying to debug this problem, I discovered the a similar behaviour can be observed if I simple attempt to dump the view object within the xml view file (../view/controller/xml/action.ctp). 'var_dump($this)' only output a partial dump of the view. The same view dump performed within the standard view file (../view/controller/action.ctp) outputs a full dump of the view.
It is my believe that Cake is somehow setting up the view environment differently when it routes for a url extension than when the standard view is requested.
Could some please shed some light on the for me before I lose my hair. Please???? Thanks!
Have you looked at the response & request objects
http://mark-story.com/posts/view/the-cakerequest-object-in-cakephp-2-0 - the article was written when cake 2 was alpha I think but Mark talks about the concept of request object (another entry for response)
both objects are passed to the controller
http://book.cakephp.org/2.0/en/controllers/request-response.html
for now I believe you use the request comp to handle layout switching (although it can be done without it)

Any alternate of drupal_json for non-JSON output

In an AJAX call back in drupal it is normally recommended to use drupal_json() to send data to client. This function converts the raw data into JSON along with HTML encoding.
I want to send the HTML data without encoding to client.
for this I am using following code:
print $html_output;
exit(0);
Is there any recommended or best way in drupal to do so?
If you need to output only the HTML output returned from the menu callback, then the following code is the correct one:
print $html_output;
module_invoke_all('exit');
exit();
If you want your output to appear together the blocks Drupal normally output, then the code needs to be changed to the following:
return $html_output;
That will do the trick. Allthough you should invoke hook_exit first. However this is shortcutting the framework some what, it may work in simple cases but wont work for forms etc.
The only time I have used this method is if I am printing some data which is allready json encoded.

Resources