5
$\begingroup$

If I want to create an API function that takes two numbers a and b and returns an image. This image is supposed to be dynamically embedded on a web page that collects a and b and passes the values to the APIFunction. However, the webpage requires the URL of the image to end with ".png" (image format), for example https://something{{a}}{{b}}.png.

I created and deployed an APIFunction and retrieved an URL https://www.wolframcloud.com/obj/myurl (i.e. the URL does not end with ".png")

What do I need to do in order to make the embedded dynamic image API work?

$\endgroup$
4
  • 1
    $\begingroup$ https://something{{a}}{{b}}.png is not a valid url ({{}}), can you add a viable example? $\endgroup$
    – Kuba
    Mar 9, 2021 at 10:51
  • 1
    $\begingroup$ 1)This cannot be achieved with an APIFunction. 2)In the APIFunction specify a and b as images. 3)In CloudDeploy of the APIFunction use "PNG" as the format of the deployed CloudObject (outside the APIFunction).4)In CloudDeploy of the APIFunction use ".png" as the named object. ( can be any string, for example wolframcloud.com/obj/myurl.png).5)In the APIFunction specify a as a "String" and drop the last 4 characters of a, then call ...myurl?a={{a}}&b={{b}}.png. 6)In the APIFunction specify b as a "String" and drop the last 4 characters of b, then call ...myurl?a={{a}}&b={{b}}.png $\endgroup$ Mar 9, 2021 at 11:10
  • $\begingroup$ This are the option I can choose $\endgroup$ Mar 9, 2021 at 11:13
  • $\begingroup$ One possible way is to create a Node.js wrapper around Wolfram API. Server.js could load the image from Wolfram API and return the same but with a link ending with jpg. Same can be achieved in PHP using URL rewriting. $\endgroup$ Mar 9, 2021 at 22:47

1 Answer 1

2
$\begingroup$

Here is an extended comment. This seems to be a task for URLDispatcher but as soon as the url contains a file extension the Cloud responds with 500 internal error.

Without file extension:

obj = CloudDeploy[
  URLDispatcher[{
    ("/" ~~ a : DigitCharacter .. ~~ "/" ~~ b : DigitCharacter .. ~~ "/image" ) :> 
      ExportForm[Rasterize @ Framed[ToExpression@a + ToExpression@b], "PNG"] 
  }],
  "myurl"
]

obj[[1]] <> "/1/5/image" // URLRead // Import

enter image description here

As mentioned with /image.png it fails.

$\endgroup$
1
  • $\begingroup$ I reported it and it is confirmed as an issue that does not not have a workaround atm. $\endgroup$
    – Kuba
    Mar 10, 2021 at 7:54

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.