Skip to content

Instantly share code, notes, and snippets.

@randyphoa
Last active November 7, 2022 19:08
Show Gist options
  • Save randyphoa/35f75a57eb7c33dff394d1457a2f8b10 to your computer and use it in GitHub Desktop.
Save randyphoa/35f75a57eb7c33dff394d1457a2f8b10 to your computer and use it in GitHub Desktop.
UI function that handles GET or POST requests
ui <- function(req) {
if (identical(req$REQUEST_METHOD, "GET")) {
htmlTemplate("index.html")
} else if (identical(req$REQUEST_METHOD, "POST")) {
body <- rawToChar(req$rook.input$read(-1))
url <- stringr::str_interp("${CPD_URL}/ml/v4/deployments/${DEPLOYMENT_ID}/predictions")
query <- list(version=VERSION)
r <- POST(
url,
add_headers(
.headers = getHeaders()
),
query=query,
body=body,
encode="raw"
)
content <- jsonlite::toJSON(
list(
fields = unlist(httr::content(r)$predictions[[1]]$fields),
values = lapply(httr::content(r)$predictions[[1]]$values, unlist)
)
)
httpResponse(
status = 200L,
content_type = "application/json",
content = content
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment