thomasp85 / fiery

A flexible and lightweight web server
https://fiery.data-imaginist.com
Other
240 stars 12 forks source link

Request Body is Empty #37

Closed guidoffm closed 6 years ago

guidoffm commented 6 years ago

Sent a POST request to the server with body:

curl -d 'Hello' -X POST http://127.0.0.1:8080

On the fiery side the body is always null. Do I have to call a certain function to get the body property filled?

thomasp85 commented 6 years ago

Yes - it is usually done with the parse() method. See ?reqres::Request for the full documentation

psolymos commented 3 years ago

Hello, running into the same issue. The fake Rook example works as expected:

fake_rook <- fiery::fake_request(
  'http://example.com/test?id=34632&question=who+is+hadley',
  content = 'This is an elaborate ruse',
  headers = list(
    Accept = 'application/json; text/*',
    Content_Type = 'text/plain'
  )
)

req <- Request$new(fake_rook)
req$parse(
    txt = parse_plain(),
    html = parse_html(),
    json = parse_json()
  )
req$body
# "This is an elaborate ruse"

However, returns empty when included in request handler: curl http://localhost:5000 -H 'application/json' -d '["Friend"]' returns ["Hello !"] instead of ["Hello Friend!"]

suppressMessages(library(fiery))
suppressMessages(library(reqres))

handle <- function(x) {
  paste0('Hello ', x, '!')
}

app <- Fire$new(host = '0.0.0.0', port = 5000L)

app$on('request', function(server, request, ...) {
  request$parse(
    txt = parse_plain(),
    html = parse_html(),
    json = parse_json()
  )
  response <- request$respond()
  response$status <- 200L
  response$body <- handle(request$body)
  response$type <- 'Application/json; charset=utf-8'
  response
})

app$ignite()

Can you please give me some pointers? I can't find examples for full apps, only for Request/Response objects. Thanks!

thomasp85 commented 3 years ago

You need to set -H "Content-Type: application/json" instead of just -H "application/json"

psolymos commented 3 years ago

So true. [slap on the forehead] Thanks.