Message Boards Message Boards

HTTPHandling`WebServer missing HTTP request body ?


Though the HTTPHandling paclet is not yet documented, I got excited by the video (from C. Barbieri & R. Di Virgilio) and I tried a test ride. I wanted to prototype a local server supporting hot reload. However I faced a really strange behavior.

The main function WebServer when used with URLDispatcher and APIFunction does not behave as expected. I will give a self-contained example below.


We define an APIFunction which returns the body of the HTTP request payload after importing it as a "JSON" string. We also create a test "JSON" string named body.

post$Test = APIFunction[{},
                s = HTTPRequestData[{"Method", "Headers", "Body"}];
body = ExportString[{"User-1"->{"Name"->"Rob","Age"->28},"User-2"->{"Name"->"Bob","Age" ->32}},"JSON"];

Now we use URLDispatcher to deploy the API to cloud. Everything works as usual and we can access the body of the POST request.

    URLDispatcher[{"/Test" -> post$Test}],
    Permissions -> "Public"
  URLBuild@{works, "Test"},
  <|"Method" -> "POST", "Headers" -> {"accept" -> "*/*", "content-type" -> "application/json"},
   "Body" -> body|>
  ] // URLExecute
{"User-1" -> {"Name" -> "Rob", "Age" -> 28}, "User-2" -> {"Name" -> "Bob", "Age" -> 32}}

However the same API fails to catch the body part of the POST request and throws an error.

serv =
  HTTPHandling`StartWebServer[URLDispatcher[{"/Test" -> post$Test}]];
  <|"Method" -> "POST","Headers" -> {"accept" -> "*/*", "content-type" -> "application/json"},
   "Body" -> body|>
  ] // URLExecute
ImportString::string: First argument None is not a string.
ImportString[None, "JSON"]

Looking at the "Main.m" file suggests that the "AsyncHandler" of the WebServer is working with a data where the body part of the HTTP request seems to be already missing. Probably someone from the developers or experts can shade some light here.

   } // SystemOpen


  • Is there a possible workaround of the above problem in the HTTPHandling context?
  • If there is a workaround, can one also POST a file (pdf,jpg,.. etc.) or other binary data and access that data from the HTTPRequestData via the APIFunction construct like above?
POSTED BY: rob imran
10 months ago

Group Abstract Group Abstract