Post to web service

Hi, 

I am trying to POST to a service service. But it seems like I am doing something wrong with the BODY. This is the part of my code that do the post

-- Submit to webservice local json = require( "json" ) local mime = require( "mime" ) local function networkListener( event )     if ( event.isError ) then         print( "Network error: ", event.response )     else         print ( "RESPONSE: " .. event.response )     end end local body = { "resource": [   {      "gpsspeed": "1",      "gpsalt": "2"    }  ] } local headers = {} headers["X-DreamFactory-Api-Key"] = "36fda24fe5588fa4285ac6c6c111111111f706d05a88" headers["Authorization"] = "Basic dXNlckBleG1111111bTp5cHpBUWVtdjhtaXM=" local params = {} params.headers = headers params.body = body network.request( "http://103.16.62.88/api/v2/cstri-sitrap/\_table/cstri.sitrap", "POST", networkListener, params )

The error, the post response, I get is

RESPONSE: {"error":{"code":400,"context":null,"message":"No record(s) detected in request. Please make sure record(s) are wrapped in a 'resource' tag. Example: {"resource":[{"record":1},{"record":2}]}","trace":["0 [internal function]: DreamFactory\\Core\\Database\\Resources\\BaseDbTableResource-\>handlePost()","1 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-core/src/Components/RestHandler.php(326): call\_user\_func(Array)","2 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-core/src/Components/RestHandler.php(187): DreamFactory\\Core\\Components\\RestHandler-\>processRequest()","3 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-core/src/Components/RestHandler.php(245): DreamFactory\\Core\\Components\\RestHandler-\>handleRequest(Object(DreamFactory\\Core\\Utility\\ServiceRequest), 'cstri.sitrap')","4 /opt/bitnami/apps/dreamfactory/htdocs/vendor/dreamfactory/df-database/src/Services/BaseDbService.php(171):

Any ideas about this? 

This is documentation for the API I am trying to POST to. 

It seems like I cant build a correct BODY in corona. Any examples or help is appreciated 

http://wiki.dreamfactory.com/DreamFactory/Tutorials/Posting_records

I think you need to JSON encode the body.  I could be wrong, but … you could try it really fast.

local json = require "json" local body = { "resource": [{ "gpsspeed": "1", "gpsalt": "2" }] } body = json.encode(body)

Please format code posts.

Also, be sure to spend time on formatting the posted code.  i.e. remove extra blank links replace tabs with spaces… or it will still be hard to read.  This post was OK, but sometimes people take my advice on using the code formatting box, but don’t make the code legible.

You can re-edit your posts as needed to improve legibility. 

Thanks formatyourcode.jpg

Hi,

Thanks for the reply and the tips around post format. I have updated the code to include what you wrote

223. local body = { "resource": [224.   { 225.     "gpsspeed": "1", 226.     "gpsalt": "2" 227.   } 228. ] 229.} 230.body = json.encode(body)

This gives a error saying, it seems like the body format is incorrect.

19:05:35.269  ERROR: Runtime error
19:05:35.269  error loading module ‘sendreport’ from file ‘C:\path\sendreport.lua’:
19:05:35.269   C:\path\sendreport.lua:223: ‘}’ expected near ‘:’
19:05:35.269  stack traceback:
19:05:35.269   [C]: in function ‘error’
19:05:35.269   ?: in function ‘gotoScene’
19:05:35.269   C:\path\menu.lua:55: in function ‘_onEvent’
19:05:35.269   ?: in function ‘?’
19:05:35.269   ?: in function ‘?’
19:05:35.269   ?: in function <?:182>
 

Thanks 

Looks like you have an extra bracket. AKA close-parentheses.  

Tip: Never assume pasted examples will just work.  Most of us are only putting them through our ‘mental compilers’.  So there can be syntax errors.

Also, I simply pasted your original code w/ a tweak, but I’d never write it like that.  I’d do this instead because it makes the hierarchy clearer for me:

local json = require "json" local body = {} body.resource = {} body.resource[1] = {} body.resource[1].gpsspeed = 1 body.resource[1].gpsalt = 2 body = json.encode(body)

I always get confused with the [ brackets embeded in { sequences. Some folks can read that just fine, but to me it always looks confusing.

Thanks! 

I updated according to your latest example, with a nice structure. I print body and it now looks like 

{"resource":{"gpsalt":2,"gpsspeed":1}}

I also added

"headers["Content-Type"] = "application/json"

and now I can see in the mySQL db that it works.

Very good! Thanks a lot for sharing your knowledge. 

Always make your code readable where possible… let the compiler do the optimisation.

This is documentation for the API I am trying to POST to. 

It seems like I cant build a correct BODY in corona. Any examples or help is appreciated 

http://wiki.dreamfactory.com/DreamFactory/Tutorials/Posting_records

I think you need to JSON encode the body.  I could be wrong, but … you could try it really fast.

local json = require "json" local body = { "resource": [{ "gpsspeed": "1", "gpsalt": "2" }] } body = json.encode(body)

Please format code posts.

Also, be sure to spend time on formatting the posted code.  i.e. remove extra blank links replace tabs with spaces… or it will still be hard to read.  This post was OK, but sometimes people take my advice on using the code formatting box, but don’t make the code legible.

You can re-edit your posts as needed to improve legibility. 

Thanks formatyourcode.jpg

Hi,

Thanks for the reply and the tips around post format. I have updated the code to include what you wrote

223. local body = { "resource": [224.&nbsp;&nbsp; { 225.&nbsp;&nbsp;&nbsp;&nbsp; "gpsspeed": "1", 226.&nbsp;&nbsp;&nbsp;&nbsp; "gpsalt": "2" 227.&nbsp;&nbsp; } 228.&nbsp;] 229.} 230.body = json.encode(body)

This gives a error saying, it seems like the body format is incorrect.

19:05:35.269  ERROR: Runtime error
19:05:35.269  error loading module ‘sendreport’ from file ‘C:\path\sendreport.lua’:
19:05:35.269   C:\path\sendreport.lua:223: ‘}’ expected near ‘:’
19:05:35.269  stack traceback:
19:05:35.269   [C]: in function ‘error’
19:05:35.269   ?: in function ‘gotoScene’
19:05:35.269   C:\path\menu.lua:55: in function ‘_onEvent’
19:05:35.269   ?: in function ‘?’
19:05:35.269   ?: in function ‘?’
19:05:35.269   ?: in function <?:182>
 

Thanks 

Looks like you have an extra bracket. AKA close-parentheses.  

Tip: Never assume pasted examples will just work.  Most of us are only putting them through our ‘mental compilers’.  So there can be syntax errors.

Also, I simply pasted your original code w/ a tweak, but I’d never write it like that.  I’d do this instead because it makes the hierarchy clearer for me:

local json = require "json" local body = {} body.resource = {} body.resource[1] = {} body.resource[1].gpsspeed = 1 body.resource[1].gpsalt = 2 body = json.encode(body)

I always get confused with the [ brackets embeded in { sequences. Some folks can read that just fine, but to me it always looks confusing.

Thanks! 

I updated according to your latest example, with a nice structure. I print body and it now looks like 

{"resource":{"gpsalt":2,"gpsspeed":1}}

I also added

"headers["Content-Type"] = "application/json"

and now I can see in the mySQL db that it works.

Very good! Thanks a lot for sharing your knowledge. 

Always make your code readable where possible… let the compiler do the optimisation.