Message Boards Message Boards

0
|
9417 Views
|
3 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Submit a list as an argument to an APIFunction?

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
3 Replies

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

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

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