0

The php guzzle OpenCPU POST request for the simple R DemoPackage function

$response = $client->request('POST','http://178.254.13.220/ocpu/library/DemoPackage/R/addition/json', ['json' => $jsontext, 'debug' => true]);

stucks during execution on a ubuntu server. The simple addition function adds 2 numbers and returns the sum as result (see code below).

If "POST" is changed to "GET", the request delivers the correct source code of the function addition in the variable body by the command : $body = $response->getBody();

On the local Mac computer, where the function was developed and tested, the DemoPackage works fine. My other R packages on the ubuntu server run perfect, however I could not find the error in this simple example of adding 2 numbers.

 addition <- function(jsontext)
 {
  tmp <- unlist(jsonlite::fromJSON(jsontext))
  a  <- as.numeric(tmp[[1]])
  b  <- as.numeric(tmp[[2]])
  c <- a + b

  aValue <- as.character(round(a))
  bValue <- as.character(round(b))
  cValue <- as.character(round(c))
  pValue_dataframe <- data.frame(aValue,bValue,cValue)
  result           <- list(sum = pValue_dataframe)

  result_json <- toJSON(result)

  return(result_json)
 }

The php code sucks while executing the POST request. With the http request debug option there is an error message, however I am not able to interpret it and or know what to do. The error message is:

Found bundle for host 178.254.13.220: 0x28d6b90 * Re-using existing connection! (#0) with host 178.254.13.220 * Connected to 178.254.13.220 (178.254.13.220) port 80 (#0) > POST /ocpu/library/DemoPackage/R/addition/json HTTP/1.1 User-Agent: GuzzleHttp/6.2.1 curl/7.35.0 PHP/5.5.9-1ubuntu4.20 Content-Type: application/json Host: 178.254.13.220 Content-Length: 67 * upload completely sent off: 67 out of 67 bytes < HTTP/1.1 400 Bad Request * Server nginx is not blacklisted < Server: nginx < Date: Thu, 04 Apr 2019 13:29:58 GMT < Content-Type: text/plain; charset=utf-8 < Transfer-Encoding: chunked < Connection: keep-alive < Access-Control-Allow-Origin: * < Access-Control-Expose-Headers: Location, X-ocpu-session, Content-Type, Cache-Control < Access-Control-Allow-Headers: Origin, Content-Type, Accept, Accept-Encoding, Cache-Control, Authorization < Access-Control-Allow-Credentials: true < X-ocpu-r: R version 3.1.3 (2015-03-09) < X-ocpu-locale: en_US.UTF-8 < X-ocpu-time: 2019-04-04 15:29:58 CEST < X-ocpu-version: 1.4.7 < X-ocpu-server: rApache < Vary: Accept-Encoding < X-Powered-By: PleskLin < * Connection #0 to host 178.254.13.220 left intact

  • The is not enough info to help. Please, run your HTTP request with the [`debug`](http://docs.guzzlephp.org/en/stable/request-options.html#debug) option enabled and add the results. – Alexey Shokov Apr 04 '19 at 11:36
  • A good advice! I added the debug option (see the text above) and changed the first 3 lines of the addition function. – Werner Lechner Apr 04 '19 at 13:42

3 Answers3

0

So your R server returns you HTTP/1.1 400 Bad Request and no more information.

I can only assume that $jsontext in your PHP code contains already prepared JSON, so in this case you need body option instead of json ('body' => $jsontext). It seems that you pack you data twice.

Alexey Shokov
  • 4,775
  • 1
  • 21
  • 22
0

Thank you for your remark. I continued my debugging this way: Before I called the request

$response = $client->request('POST','http://178.254.13.220/ocpu/library/DemoPackage/R/addition/json', ['json' => $jsontext, 'debug' => true]);

I set for debugging the variable jsontext inside the R function to a fixed value and the function addition now looks like

addition <- function(jsontext)
{
 jsontext <- '{ "jsontext" : [{"a" : "5.1", "b" : "8.9"}]}'     
 ...
 return jsontext
}

Now, the results are correct and everything runs fine. Therefore is seems clear, that I did set the variable $jsontext inside the php source in a wrong way by using this expressions:

$data     = '{ "jsontext" : [{"a" : "5.1", "b" : "8.9"}]}' ;
$jsontext = json_decode($data); 
require "vendor/autoload.php";
$client = new GuzzleHttp\Client();
$response = $client->request('POST','http://178.254.13.220/ocpu/library/DemoPackage/R/addition/json', ['json' => $jsontext, 'debug' => true]);

I hope, that anybody is familiar with the php and R syntax and would help me out with correct expressions.

0

After many hours of debugging and reading several docs I could solve my problem. The error occurred in the first line of the R function addition:

addition <- function(jsontext)
{
 # this was the wrong expression
 tmp <- unlist(jsonlite::fromJSON(jsontext))

 # this is the correct expression
 tmp <- c(unlisted(jsontext))

 ...

 ...

It is strange, because in the php source the parameter $jsontext of the function addition was set in the JSON format like

$data     = '{ "jsontext" : [{"a" : "5.1", "b" : "8.9"}]}' ;
$jsontext = json_decode($data);

Thanks to all who did try to help!