7
$\begingroup$

I have a function whose argument is Audio and I'd like to deploy it as an API (and then call it from Python). This works as with FormPage[] as follows:

CloudDeploy[
 FormPage[{{"ex", "Upload WAV file:"} -> "UploadedFile"}, 
  AudioPlot@Import[#ex] &]]

But I'm not sure how to set it up with APIFunction[] and then call it as a HTTP GET request. I can assume they are WAV files if that makes it easier.

$\endgroup$

1 Answer 1

6
$\begingroup$

You can create an APIFunction with a very similar syntax and return e.g. a png"

CloudDeploy[
  APIFunction[
    {{"ex", "Upload WAV file:"} -> Restricted["UploadedFile", "WAV"]}
  , HTTPResponse[ExportString[AudioPlot@Import[#ex, "WAV"], "PNG"]] &
  ]
, "api/audioPlot"
, Permissions -> "Public"
];

Can't say how to call it from Python but from WL it goes like this:

req = HTTPRequest[
  CloudObject["api/audioPlot"]
, <|"Body" -> {"ex" -> File[path/to/file.wav]}|>
];

URLExecute[req, "PNG"]

enter image description here

$\endgroup$
2
  • $\begingroup$ Hi, this answer doesn't seem to work in V12, the URLExecute now returns a Failure object with the message "Import::fmterr: Cannot import data as PNG format." $\endgroup$
    – M.R.
    Commented Sep 25, 2019 at 16:03
  • $\begingroup$ Has something changed in APIFunction or URLExecute? $\endgroup$
    – M.R.
    Commented Sep 25, 2019 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.