vendredi 9 août 2019

Rserve raw vector in response payload

How can I send raw vector to the response body in the Rserve http web server?

I was guided of this Simon's answer about .http.request result structure.

My toy web application looks as following:

#!/usr/bin/env Rscript

## ---- HTTTP request handler ----

.http.request = function(path, query, body, headers) {
  resp_body = "Hello, World!"
  resp_headers = ""
  content_type = "text/plain"
  status_code = 200
  list(
    charToRaw(resp_body),
    content_type,
    resp_headers,
    status_code
  )
}


## ---- start application ----

Rserve::run.Rserve(
  http.port = 8001
)

To start application do the following:

Rscript app.R


Trying to fetch image:

$ curl -v http://127.0.0.1:8001/ -o /tmp/plot.png
*   Trying 127.0.0.1:8001...
* TCP_NODELAY set
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to 127.0.0.1 (127.0.0.1) port 8001 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8001
> User-Agent: curl/7.65.3
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-type: image/png
* no chunk, no close, no size. Assume close to signal end
<
{ [8116 bytes data]
100  8116    0  8116    0     0     43      0 --:--:--  0:03:08 --:--:-- 




Aucun commentaire:

Enregistrer un commentaire