Vous êtes sur la page 1sur 3

HTTPClient, JSONGenerator and JSONParser

For dealing with more complex return data structures - and for dealing with updates - we can use the
HTTPClient, JSONGenerator and JSONParser. First we're going to retrieve that same data from that
same REST web service, but doing it with the HTTPClient. This involves a bit more code:

integer li_rc
long ll_id, ll_userid,ll_row,ll_root,ll_index,ll_child,ll_count
string ls_string,ls_result,ls_body,ls_title
httpclient http
http = create httpclient
li_rc = http.sendrequest( 'GET',
'https://jsonplaceholder.typicode.com/posts')
if li_rc = 1 and http.GetResponseStatusCode() = 200 then
http.GetResponseBody(ls_string)
end if
JSONParser json
json = create JSONParser
ls_result = json.loadstring( ls_string )
ll_root = json.getrootitem( )
ll_count = json.getchildcount( ll_root )
dw_1.Reset()
for ll_index = 1 to ll_count
ll_row = dw_1.InsertRow(0)
ll_child = json.getchilditem( ll_root, ll_index )
ll_id = json.getitemnumber( ll_child, "id" )
dw_1.setItem(ll_row, "id", ll_id)
ll_userid = json.getitemnumber( ll_child, "userid" )
dw_1.setItem(ll_row, "userid", ll_userid)
ls_title = json.getitemstring( ll_child, "title")
dw_1.setItem(ll_row, "title", ls_title)
ls_body = json.getitemstring( ll_child, "body")
dw_1.setItem(ll_row, "body", ls_body)
next

We use the HTTPClient SendRequest method to call the REST service method, check the response
status code using GetResponseStatusCode and, if success, get the data in JSON format using
GetResponseBody. We will then use the JSONParser to parse the JSON data, looping through it and
sticking individual data elements in individual rows and columns.

Now lets see how we can perform updates through the REST web service from the DataWindow using
those objects:

integer li_rc, li_rsc


long ll_index, ll_count, ll_id, ll_root
constant integer OK = 200
constant integer CREATED = 201
String ls_json, ls_result
dwItemStatus status
HttpClient hc
JSONGenerator jg
JSONParser jp
hc = create HttpClient
jg = create JSONGenerator
jp = create JSONParser
dw_1.AcceptText()
ll_count = dw_1.Rowcount( )
FOR ll_index = 1 TO ll_count
status = dw_1.GetItemStatus ( ll_index, 0, Primary! )
CHOOSE CASE status
CASE NewModified!, DataModified!
//Inserted or Modified Rows
ll_root = jg.createjsonobject( )
jg.AddItemNumber(ll_root, "userid", dw_1.GetItemNumber(ll_index,
'userid'))

jg.AddItemString(ll_root,'title',dw_1.GetItemString(ll_index,'title'))

jg.AddItemString(ll_root,'body',dw_1.GetItemString(ll_index,'body'))
IF status = NewModified! THEN
//Inserted
ls_json = jg.getjsonstring( )
li_rc = hc.sendrequest( 'POST',
'https://jsonplaceholder.typicode.com/posts', ls_json)
li_rsc = hc.GetResponseStatusCode()
IF li_rsc = CREATED THEN
//Get the response, which contains the ID value assigned
to the new row
hc.GetResponseBody(ls_json)
ls_result = jp.loadstring( ls_json )
ll_root = jp.getrootitem( )
ll_id = jp.getitemnumber( ll_root, "id" )
//Set it back into the inserted row
dw_1.SetItem ( ll_index, 'id', ll_id )
ELSE
MessageBox ( parent.title, "Insert Failed" )
END IF
ELSE
//Updated
ll_id = dw_1.GetItemNumber ( ll_index, 'id' )
jg.AddItemNumber(ll_root,"id", ll_id)
ls_json = jg.getjsonstring( )
li_rc = hc.sendrequest( 'PUT',
'https://jsonplaceholder.typicode.com/posts/' + String ( ll_id ), ls_json)
li_rsc = hc.GetResponseStatusCode()
IF li_rsc <> OK THEN
MessageBox ( parent.title, "Update Failed" )
END IF
END IF
CASE ELSE
//skip this row
CONTINUE
END CHOOSE
NEXT
//Deleted Rows
ll_count = dw_1.Deletedcount( )
FOR ll_index = 1 TO ll_count
ll_id = dw_1.GetItemNumber(ll_index, 'id', Delete!, true)
li_rc = hc.sendrequest( 'DELETE',
'https://jsonplaceholder.typicode.com/posts/' + String ( ll_id ))
li_rsc = hc.GetResponseStatusCode()
IF li_rsc <> OK THEN
MessageBox ( parent.title, "Delete Failed" )
END IF
NEXT
dw_1.ResetUpdate( )

NOT MINE. THIS WAS TAKEN FROM https://www.brucearmstrong.org/2018/02/powerbuilder-2017-r2-


new-feature-rest.html

ALL CREDITS GO TO ITS PROPER AUTHOR

Vous aimerez peut-être aussi