Message Boards Message Boards

Submit a list as an argument to an APIFunction?

GROUPS:

When I attempt to run this APIFunction with the argument {1,2,3,4} it does not work. How do I pass this argument to the function?

CloudDeploy[APIFunction[{"d" -> "String"}, Min[#d] & , "String"]]
POSTED BY: Arnold Stillman
Answer
8 months ago

There are two ways to go about this:

api1 = APIFunction["x" -> RepeatingElement["Integer"], Max[#d]&]
api2 = APIFunction["x" -> DelimitedSequence["Integer"], Max[#d]&]

and then

api1[{"x" -> "1", "x" -> "2", "x" -> "3", "x" -> "4"}]
api1[{"x" -> {"1", "2", "3", "4"}}]
api2["x" -> "1, 2, 3, 4"]

So the difference between RepeatingElement and DelimitedSequence is that the first takes repeated keys, while the second will split a string for you.

POSTED BY: Carlo Barbieri
Answer
8 months ago

Thanks. This answers my question. My input is actually json, so in desperation I solved my problem by importing the json directly with

APIFunction[{"d" -> "JSON"},  ToExpression[Take[Sort[Keys[#d]] /. #d, 9]] & , "String"]
POSTED BY: Arnold Stillman
Answer
8 months ago

There is a much better way of doing that

APIFunction["d" -> RepeatingElement["Integer"], Max[#d] &]["d__json" -> "[1,2,3,4]"]

Look at the documentation of AllowedCloudParameterExtensions, to see why that works, and what else can be made to work.

Or another way of doing it is to use DelimitedSequence to define a JSON list parser for you:

APIFunction["d" -> DelimitedSequence["Integer", {"[", ",", "]"}], Max[#d] &]["d" -> "[1,2,3,4]"]
POSTED BY: Carlo Barbieri
Answer
8 months ago

Group Abstract Group Abstract