Home Ask Login Register

Developers Planet

Your answer is one click away!

Luca Poddigue February 2016

Google Cloud Endpoints allowed method's return types

I'm developing a REST API using Cloud Endpoints. I know that as per the documentation each API method should return an entity that is then automatically converted to a valid JSON string. However, I'm dealing with an authentication library that in some cases returns a JSON which should be passed back to the client as a response.

Sticking with the default approach, meaning returning an entity, would still be possible, but it would involve a number of obnoxious intermediate steps, like parsing the JSON and filling the right fields of the entity to be returned according to the JSON content.

I was wondering if there is a more straightforward way to instruct the API to directly return the JSON string, instead of converting it to an entity just to have it translated back to the source JSON.


Ankur Choraywal February 2016

One thing that probably may help you. Suppose you have a RPC response class as

from protorpc import messages
from protorpc import message_types
from protorpc import remote

class ApiResponse(message.Messages):
 response = messages.StringField(1, required = True)

You can return your JSON in response field by using JSON module. First of all import json in your endpoints api

import json

Suppose your response is

json_response = [{'name': 'Jack', 'age':12}, {'name': 'Joe', 'age':13}]

In your response to this api you can do this:-

return ApiResponse(response = json.dumps({'data': json_response}))

json.dumps() converts your dictionary object into JSON string that can be passed to response of ApiResponse class.

After you receive response in client side(javascript), you can simply parse it to JSON using


Post Status

Asked in February 2016
Viewed 2,417 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life