I am calling the stripe api from the cloud code using the code below, the body is returned as a string, is there anyway I can decode that in the cloud api rather than sending it back to Corona and getting Corona to do it (which does work), basically I want to call json.decode on the body so I have the information in the cloud api.
The reason is I’m trying to do this is to minimize the backwards and forwards of many calls to the server.
local api\_key = "sk\_test\_1234567890" local description = "Test" local answer = coronium.user.getUser( in\_data.userid ) if not answer.error then local email = answer.result.email coronium.log( answer.result.email ) local newCustomer = "email="..email.."&description="..description local header = { ["Authorization"] ="Bearer "..api\_key, } local answer = coronium.network.request( 'https://api.stripe.com/v1/customers', newCustomer, 'POST', header ) coronium.log( type(answer.result.body) ) coronium.output( {result = answer.result.body} ) else coronium.output( answer ) end
This method posts and returns a Lua table converted from the JSON response.
You might wonder why there is seemingly no “encode/decode” method. That was a design decision because Coronium relies on a unified pattern of decode/encode throughout the system, so that the components can easily inter-operate. This is done automatically. If a “manual” conversion is not handled correctly and gets pushed back into that pipeline, failures will occur. Hence the above method, and the “backgrounding” of the lower level calls.
Coronium uses cjson in a “safe” way. This means that Coronium won’t throw a runtime error if it cannot convert the data. Instead you will receive the standard “Answer” table with the errorString telling you why it would not convert.
If you need access to this at a lower level its:
[lua]
local answer = coronium.json.encode( data )
if answer.error then
print( answer.errorString )
else
print( answer.result.some_var )
end
[/lua]
and to decode…
[lua]
local answer = coronium.json.decode( data )
– see above code
[/lua]
I would advise removing the dkjson. The consequences are yours, and yours alone.
This method posts and returns a Lua table converted from the JSON response.
You might wonder why there is seemingly no “encode/decode” method. That was a design decision because Coronium relies on a unified pattern of decode/encode throughout the system, so that the components can easily inter-operate. This is done automatically. If a “manual” conversion is not handled correctly and gets pushed back into that pipeline, failures will occur. Hence the above method, and the “backgrounding” of the lower level calls.
Coronium uses cjson in a “safe” way. This means that Coronium won’t throw a runtime error if it cannot convert the data. Instead you will receive the standard “Answer” table with the errorString telling you why it would not convert.
If you need access to this at a lower level its:
[lua]
local answer = coronium.json.encode( data )
if answer.error then
print( answer.errorString )
else
print( answer.result.some_var )
end
[/lua]
and to decode…
[lua]
local answer = coronium.json.decode( data )
– see above code
[/lua]
I would advise removing the dkjson. The consequences are yours, and yours alone.