Group Abstract Group Abstract

Message Boards Message Boards

1
|
6.5K Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

What types of HTTP POST an API function in the Cloud can deal with?

Posted 3 years ago

Hello,

I successfully deployed a public API Function[] in Wolfram's Cloud.

Working with HTTP POST procedure, I can effectively post data using Form URL Encoded content-type (Like : param1=value1&param2=value2) toward the API URL, and the API does its job nicely.

Sadly, I'm dealing with an external service that also uses HTTP POST using either plain text or JSON.

JSON being an industry standard for API stuff for some time now, and also being very easy to manipulate, I was happy to deal with this type of data.

But I don't succeed.

This is my code example :

api=APIFunction[{"Chiffre"->"String"},StringSplit[#Chiffre]&]
(* Testing the API : Strings are splitted after each space, returns : {"Il","neige","au","Printemps"} *)
api[<|"Chiffre"->"Il neige au Printemps"|>]
(* Deploying the API in the Cloud *)
CloudDeploy[api,Permissions->"Public"]

Using a HTTP GET works fine (adding ?Chiffre="Il neige au Printemps" in the URL), and so does HTTP POST using Form URL Encoded (sending Chiffre="Il neige au Printemps").

But sending a JSON content fails to be treated by the API. My JSON request is :

{"Chiffre": "Il neige au Printemps"} 

I get this response from API :

{
    "Success": false,
    "Failure": "The API could not be evaluated because there is no input for fields: \"Chiffre\".",
    "Fields": {
        "Chiffre": {
            "AllowedExtensions": [
                "json"
            ],
            "AutoSubmitting": false,
            "CodeLanguage": "Automatic",
            "Default": null,
            "Enabled": true,
            "Failure": "This field is required.",
            "Help": null,
            "Hidden": false,
            "Hint": null,
            "Input": null,
            "Interpreter": "String",
            "Label": "Chiffre",
            "Required": true,
            "Type": "Element"
        }
    }
}

Can somebody suggest something for dealing with JSON as input for cloud-hosted API ? Or should I use plain text and then parse the information inside the API ?

Thank you.

POSTED BY: Clarisse Wagner
3 Replies
Posted 22 days ago

It sounds like you want the APIFunction to use the request body and parse it as JSON.

The first argument to APIFunction is the list of parameters, which are obtained either from query parameters or as you first indicated, or from parameters in the request body using application/x-www-form-urlencoded or multipart/form-data. APIFunction and FormFunction automatically detect these from the HTTP request to populate the parameters.

This explains the error you got, about not finding the "Chiffre" parameter.

You can do what you want, you just need to access the request body (through HTTPRequestData) and parse it (through ImportString), and additionally, not declare any parameters (by providing an empty list as the first argument). Here's the end-to-end code:

api = CloudPublish[
  APIFunction[{}, 
   StringSplit[
     ImportString[FromCharacterCode[HTTPRequestData["BodyBytes"]], 
       "RawJSON"]["Chiffre"]] &]]
requestData = {"Chiffre" -> "Il neige au Printemps"};
requestBody = ExportByteArray[requestData, "JSON"];
URLRead[HTTPRequest[api, <|"Body" -> requestBody|>], "Body"]

Additionally, if you want to test the APIFunction without the cloud, you would use GenerateHTTPResponse, and supply a 2nd argument so you can specify the request body:

GenerateHTTPResponse[
  APIFunction[{}, 
   StringSplit[
     ImportString[FromCharacterCode[HTTPRequestData["BodyBytes"]], 
       "RawJSON"]["Chiffre"]] &],
  <|"BodyBytes" -> 
    ToCharacterCode[
     ExportString[{"Chiffre" -> "Il neige au Printemps"}, "JSON"], 
     "UTF-8"]|>
  ]["Body"]
POSTED BY: Joel Klein

Pinging this old discussion to note that Wolfram's APIFunction only accepts the application/x-www-form-encoded content type. It doesn't accept JSON directly.

The only alternative I can think of within Wolfram is using Wolfram Data Drop, which does accept JSON input.

POSTED BY: Steven Buehler
Posted 1 year ago

Did you manage to crack the code by now?

POSTED BY: Smoothur Alex
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard