Handling a JSON Input in a POST Request

Hi everyone,

I have developed a Dockerized R API that receives a JSON with multiple inputs, performs data operations, and returns a list as output.

The API works on a local machine and deployed on a workstation without any problem. However, when I deploy it on Azure Web App POST request returns: 'No JSON file is found in the request' error, while, I do not experience any issue with the GET (echo) endpoint.

The Web App allows inbound traffic, hence, I deduce the issue is most probably because of how I handle the request.

I have several questions:

  1. How can I manipulate the code so that the JSON input is a parameter of a POST endpoint and will be processed under that?

  2. What could be the other potential reasons of such strange behavior in Azure Web App?

Thanks

library(plumber)
library(jsonlite)
library(reprex)

source('./functions.R')

#* @apiTitle ...
#* @apiDescription ...
#* @apiDescription ...
#* @apiVersion 1.0

#* @plumber
function(pr) {
  pr %>%
    pr_set_debug(TRUE)
}

#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function() {
    list(Status = 'API is working properly!',
         Time = Sys.time())
}

#* Log some information about the incoming request
#* @filter logger
function(req){
  cat(as.character(Sys.time()), "-",
      req$REQUEST_METHOD, req$PATH_INFO, "-",
      req$HTTP_USER_AGENT, "@", req$REMOTE_ADDR, "\n")
  
  forward()
}

#* Get model input
#* @filter /field_filter
function(req, res){
  
  if(grepl("field_filter", req$PATH_INFO)){
    req$data <- tryCatch(jsonlite::fromJSON(req$postBody), 
                         error = function(x){
                           return(NULL)
                         })
    
    if(is.null(req$data)){
      res$status <- 400
      return(
        list(error = "No JSON file is found in the request")
      )
    }
    
    list_of_inputs <- req$data
    
    req$results <- main(input_1 = list_of_inputs[[1]],
                                             input_2 = list_of_inputs[[2]],
                                             input_3 = list_of_inputs[[3]])
  }
  
  forward()
  
}

#* ...
#* @post /field_filter/report
function(req){
  
  res <- req$results
  res
}

Did you set the content header to your Azure Web App content-type : "application/json"?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.