Get and send data from existing REST service

Now that I have WEBcore installed, am looking for the best way to adapt to my workflow and have been reading the documentation.  


- Connect to an existing REST webservice (I developed the services and they are quite stable) 
  The REST request has data in the headers for authentication and the URL plus the Body payload tells if the service will use the data or if it has to return data, as in any RESTful service.
- Data sets come in JSON. I would like to use one of the webcore components to pick the JSON and map the fields as a data source so a grid or edits can be filled up without having to parse the JSON field by field, in other words, the delphi way.

- For sending data to the webservice, I would like to take the data fields modified by the user, and have the component take the fields, convert it to a Json and send it in a call to the REST webservice, which based on the endpoint already will know is a request to save data.

So far I  have not found examples or something close to that. and I would really prefer not to have to develop the server side again...

Which are the best components in web core suited for something like this, or at least, to make an asynchronous call where I can retrieve or send a Json  in http requests?  The agility of the SPA and handling everything in the client side is great.  Today am using AJAX with plain javascript for the purpose and hope to be able to convert the project to Webcore using pascal. 

The TWebHttpRequest can be used for this. You can directly set headers via TWebHttpRequest.Headers.


If you use a TWebClientDataSet and the data you receive is a JSON array, you can assign this array to WebClientDataSet.Rows. 
This dataset can be connected via a datasource to your DB-aware controls.
From the dataset events, you could create the desired JSON to send back in case of updates/delete/insert.



Great, that works for me, thank you. 


Hi Bruno,

I know this is an old post but I basically have the same scenario as above.

However, I have a function that sends the JSON returned from my Web Service to a 'Parser' that returns a string of JSON based on the element it is looking for. (It could return a JSONObject as well).

  PAS_Object := TJSONObject.ParseJSONValue(SampleObjectData) as TJSONValue;
  sJson := (ParseJson(PAS_Object.ToString, 'Original'));
  ja := TJSONObject.ParseJSONValue(sJson) as TJSONArray;

An example of the string that is return is as follows:

{"INV_ID":1063,"PART_NUMBER":"6350130","DESCRIPTION":"5\" X 5\" STIRRUPS","UNIT_OF_MEASURE":"EA","PART_TYPE":"P","KEEP_FG_INVENTORY":"F","ClassDesc":"REBAR & STIRRUPS","QTY_ON_HAND":289,"RETAIL_UNIT_PRICE":2.95,"RETAIL_MBF_PRICE":2.95,"WEIGHT":1}

I can get this data into a TJSONArray but the WebClientDataSet is looking for a TJSArray.

I am not sure how to get my returned data into my WebClientDataSet as I am not sure how to convert my data into a TJSArray.
Do you have any suggestions?

Is this one record you want to add to the dataset?

This example happens to be one record but there could be multiple records returned. Basically, I parse a nested JSON file to get to the particular array of interest. I then want to load the records from the array into a Dataset.

Sorry, Bruno.

That wasn't a very good example of the returned data.
Here is another example:

[{"RowID":0,"Original":{"CUSTOMER_ID":204,"NAME":"CASH SALES","BILL_ADDRESS_1":"CASH SALES","BILL_CITY":"EDMONTON","BILL_PROVINCE":"AB","BILL_POSTAL":"","CURRENCY_ID":1,"CUST_DIV":"01","TELEPHONE":"","SALESPERSON":"DEBBIE","TERMS":"NET 30TH M.F.","CREDIT_LIMIT":1000000,"CREDIT_USED":17479.87,"CREDIT_HOLD":"F","INPUT_HOLD":"F","RETAIL_CUSTOMER":"T","DEF_DELIVERY_PCT":0,"DEF_DELIVERY_CHARGE":135}},{"RowID":1,"Original":{"CUSTOMER_ID":1951,"NAME":"MORRISON HOMES (EDMONTON) LTD.","BILL_ADDRESS_1":"11158 - 42 STREET SE","BILL_CITY":"CALGARY","BILL_PROVINCE":"AB","BILL_POSTAL":"T2C 0J9","CURRENCY_ID":1,"CUST_DIV":"01","TELEPHONE":"403-279-7600","FAX":"403-236-0311","SALESPERSON":"GREGG","TERMS":"NET 30TH M.F.","CREDIT_LIMIT":300000,"CREDIT_USED":0,"CREDIT_HOLD":"F","INPUT_HOLD":"F","RETAIL_CUSTOMER":"F","DEF_DELIVERY_PCT":0,"DEF_DELIVERY_CHARGE":0}}]

If it is a JSON array, you can load the JSON array in a dataset via

webclientdataset.Rows: TJSArray

My problem is that my data is in a TJSONArray.
It will not let me load the TJSONArray because it is looking for TJSArray.
Based on the example above, how do I get my data into a TJSArray?

Parse the JSON string with TJSJSON and cast to TJSArray

var
  ja: TJSArray;
begin
  ja := TJSArray(TJSJSON.parse('myjsonstring'));
end;
1 Like

Thanks, Bruno.

That is exactly what I was looking for.