Pass JSON Dataset to a Rest Server

mskeels

Member
Licensed User
Longtime User
Newbie (still) here.

:sign0104:

I realize this is probably not the ideal place to ask this question, but here goes.

Assuming I create a JSON string in my B4A app containing several rows of data to be passed to a web server for storage in the server side db, can anyone give me a clue as to how I would do that from the app?

What I am looking for here is how to structure the HTML, like, would I just pass the entire JSON string as a parameter?

ie

http://(blahblahblah)/myJSONString/

????

Thanks,
Mark
 

abner69

Member
Licensed User
Longtime User
Mark,
using the httputils2, i recommend that you use the POST message to post to your server.

Don't forget that you will need to edit your server configuration to set a max size of your post message (the max size of you json string)

...Pablo
 
Upvote 0

mskeels

Member
Licensed User
Longtime User
So assuming a correctly constructed JSON string, does this look correct?

B4X:
Sub Button1_Click
Dim result, What As String
Dim URL As String

   result = Utils.BuildInspPointRecsJSONString(SQLrfid)

   URL="http://" & Utils.ListPersistent.Get(Utils.SERVER_IP) & ":" & _
   Utils.ListPersistent.Get(Utils.SERVER_PORT) & "/datasnap/rest/TServerMethods1/UpdateStorage/"
   What = OPDetailsJob.PostString(URL, "Key=Rows&Data="&result)
   
   
End Sub

???


Thanks,
Mark
 
Upvote 0

abner69

Member
Licensed User
Longtime User
it is much easier to just use the HttpUtils2 framework to post and then process the result. You can find more information on httputils2 here.

Let me know if i can be of further assistance.

...Pablo
 
Upvote 0

mskeels

Member
Licensed User
Longtime User
Ok, so here's what I did.

First of all, I am using HTTPUtils2...it's just not obvious from the subroutine code I posted.

Being new to all this HTTP stuff, it took me quite a while to figure out exactly how to construct my HTTP for the PostString method.

B4X:
Sub Button1_Click
Dim result, What As String
Dim URL As String

   result = Utils.BuildInspPointRecsJSONString(SQLrfid)

   URL="http://" & Utils.ListPersistent.Get(Utils.SERVER_IP) & ":" & _
   Utils.ListPersistent.Get(Utils.SERVER_PORT) & "/datasnap/rest/TServerMethods1/Storage/Rows"
   Log(result)
   What = OPDetailsJob.PostString(URL, result)
   OPDetailsJob.GetRequest.SetContentType("application/json")
   
End Sub

For anyone who is trying to do what I am trying to do, the following will be helpful.

The "server" in this case is a Delphi Datasnap REST server.

It handles POSTs according to this link:

DataSnap REST Messaging Protocol - RAD Studio XE3

There they note that the Server method on the Delphi side will convert the JSON to a JSON object of type TJSONValue.

I was confused by the need to supply a key and an ID in the URL, but after looking at this thread

http://www.b4x.com/forum/basic4andr...8-httputils2service-bas-application-json.html

I saw that all I needed to do was to pass in the JSON string as the second parameter of the PostString method. The first parameter of the PostString method was my URL, with the "Key" value appended to the end. In this case, "Rows." That is also the name of the outer JSON object in the JSON string.

Finally, I had to add the line

OPDetailsJob.GetRequest.SetContentType("application/json")

To tell the server that this is JSON data.

Finally it worked.

I guess this is how it goes when you start from ground zero with B4A, HTTP, JSON and Datasnap servers in Delphi.

Oh, and another thing, I guess servers (or browsers?) somehow always prepend the text "Update" to a Post message? So when I specify my server method in the HTTP line, I eliminate the text "Update" at the start of the method name and it somehow magically adds that to the call, arriving at the correct method in the Delphi app. Go figure.

But yes, it works, so thanks Pablo and all.

This B4A is so productive, flexible and easy to use. I really like it.

Mark
 
Last edited:
Upvote 0

mskeels

Member
Licensed User
Longtime User
Oops....one more thing......be sure to set the JobName. Because this call will fire the JobDone method and if you don't set the name it will contain the last name used.

:)

B4X:
Sub SendInspPts
Dim Result As String
Dim URL As String

   Result = Utils.BuildInspPointRecsJSONString(SQLrfid)

   URL="HTTP://" & Utils.ListPersistent.Get(Utils.SERVER_IP) & ":" & _
   Utils.ListPersistent.Get(Utils.SERVER_PORT) & "/Datasnap/Rest/TServerMethods1/Storage/" & _
      Utils.SESSIONID & "/" & _
   Utils.ListPersistent.Get(Utils.DEV_IP) & "/" & _
   Utils.ListPersistent.Get(Utils.DEV_ID) & "/ROWS"

   Log(Result)
   OPDetailsJob.JobName = "SendInspPts"
   OPDetailsJob.PostString(URL, Result)
   OPDetailsJob.GetRequest.SetContentType("Application/JSON")
End Sub
 
Upvote 0
Top