Message Boards Message Boards


How to invoke Instant API using HTTP POST?

Posted 9 years ago
3 Replies
1 Total Likes

Suppose I have deployed this function to the cloud:

AddTwo[n1_, n2_] := n1 + n2;

I know how to invoke the API using an HTTP GET: http://<my url>/AddTwo?n1=2&n2=2. What Content-Type and request body do I need to send in an HTTP POST in order for the API to be invoked correctly? I cannot find any examples of this in the documentation, and I have not been able to use the Instant API "Test" function successfully with the HTTP POST radio button enabled.

Thanks, Nate

POSTED BY: Nathan Brixius
3 Replies

Yes, URLExecute does work for "POST" requests. As an example, try using it with a deployed FormFunction:

In[5]:= co = CloudDeploy[FormFunction["x" -> "Number", #x &]]

Out[5]= CloudObject["\

In[6]:= URLExecute[co, {"x" -> 4}, "Method" -> "POST"]

Out[6]= 4

Using the default method ("GET"), the form is returned instead of the result.

In[9]:= URLExecute[co, {"x" -> 4}]

Out[9]= "x

POSTED BY: Chad Knutson

Chad et al,

Do you know if URLExecute is able to handle "Method"->"POST"? It is unclear in the documentation.

Thank you, Diana

POSTED BY: Wei-Li Diana Ma

Unfortunately, the API "Test" for POST is broken (and has never worked correctly). The good news is that it's not hard to do with Wolfram Language.

The Content-Type can be text/plain, since I'm assuming that you want to add numbers. It can be set to anything that Interpeter can handle (e.g., for images, image/jpeg).

Using URLFetch, we can specify the method as POST. The full call looks like this:

URLFetch["", "Method" -> "POST", 
 "MultipartElements" -> {{"n1", "text/plain", ToCharacterCode@"2"}, {"n2", "text/plain", ToCharacterCode@"3"}}, 
 "VerifyPeer" -> False]

Note that if you're using a version of Mathematica prior to 10.0.2, you need to change "MultipartElements" to "MultipartContent".

POSTED BY: Chad Knutson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract