Support Topics Documentation Slack YouTube Blog

Which parameter should be declared for service method to accept JSON Body


(Okwui Agada) #1

I am trying to create a service that will accept a JSON String via a post, parse the JSON String into model(s) and then save them on the backend.
My problems -

  1. How do I define the service method to accept the JSON body? IS it just to define a string parameter and then do the parsing in the code?
  2. If I specify a class in the Model as the parameter, when I check the API in the console, it appears to be able to accept a JSON body. but I keep getting errors like {code=1009, message=Table not found by name ‘Table not found by name ‘services’. Make sure to check the name of the table in Backendless console and use the same capitalization in writing’, errorData={tableName=Table not found by name ‘services’. Make sure to check the name of the table in Backendless console and use the same capitalization in writing}}. Of course I do not have a table called sevices though there are relations named services that are in the JSONBody sent.

Which approach works?
Rgds


(Mark Piller) #2

You should be able to define it as a String, however, in that case (as you pointed out), you’d need to parse the JSON string using your own code.

With regards to (2), could you provide an example of how you declare your service method?

Regards,
Mark


(Okwui Agada) #3

Actually the problem in 2 is my fault. I gave the wrong url and services was being treated as a table.
If I just specify the parameter as a model class, it is parsed and available automatically to the method. However, it appears that I have to find a way to return a json body. Returning an entity does not seem to wok.


(Mark Piller) #4

If your service returns just an object and you invoke your service via REST, the response will be a JSON object.