[Solved] CloudDeploy of an APIFunction not working properly

My code is as follows:

apiFunc[opt_, f_] := Switch[
    opt,
    "derivative", FullForm[Collect[ Derivative[f, x], x, FullSimplify]],
    "integrate", FullForm[Collect[ Integrate[f, x], x, FullSimplify]],
    "nabla", FullForm[Collect[(D[#,x]&) /@ f, x, FullSimplify]],
    _, "invalid option"
];

func = APIFunction[{"opt"->"String","f"->"Expression"}, apiFunc[#opt,#f] &];
api = CloudDeploy[func]

When I run apiFunc["nabla", List[ArcSin[x]]] on the notebook, it gives the desired result:

List[Power[Plus[1,Times[-1,Power[x,2]]],Rational[-1,2]]]

However, when I get the response from the web api, it gives

FullForm[{1/Sqrt[1 - x^2]}]

Why are they different? Is there any way to get the desired result on the web api?

I have the basic plan though. Does it matter?

Maybe not a problem with CloudDeploy.. wolframscript shows a similar problem:

$ wolframscript
Wolfram Language 12.1.0 Engine for Linux x86 (64-bit)
Copyright 1988-2020 Wolfram Research, Inc.

In[1]:= Collect[D[3 x^2 + 6 x - 45 Exp[x], x], x, FullSimplify] // FullForm          

Out[1]//FullForm= Plus[6, Times[-45, Power[E, x]], Times[6, x]]

In[2]:=                                                                              
$ wolframscript -code 'Collect[D[3 x^2 + 6 x - 45 Exp[x], x], x, FullSimplify] // FullForm'
FullForm[6 - 45*E^x + 6*x]

Update: I found a similar problem: https://mathematica.stackexchange.com/questions/199991/texform-doesnt-work-in-wolframscript

Right, as the docs say, FullForm is a printing construct, so you can see it (a) in an output cell in a notebook (b) in an output of the command-line kernel, (c) when you pass it to ToString which uses OutputForm by default.

To fix the above so you get the full-form, I would make two changes:

  1. wrap ToString around your apiFunc call to turn the FullForm into a string

  2. use the 3rd argument of APIFunction to specify that you want the "Text" export format (the default is “WL” a.k.a. “Package”, which gives an InputForm rendering of the expression)

     func = APIFunction[{"opt" -> "String", "f" -> "Expression"}, 
       ToString[apiFunc[#opt, #f]] &, "Text"]