Message Boards Message Boards

0
|
3434 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

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

Posted 2 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
2 Replies
Posted 2 months ago

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 11 months 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

Group Abstract Group Abstract