Another REST client demo that I wrote for recent conferences is a VCL client accessing to some music information, different than the demo I blogged about last week. The new demo, in fact, uses the same service but makes a different request, as you can see from the following REST client components settings:
object RESTClient1: TRESTClient BaseURL = 'http://api.discogs.com' end object RESTRequest1: TRESTRequest Client = RESTClient1 Params = < item Kind = pkURLSEGMENT name = 'NAME' Options = [poAutoCreated] Value = 'Michael Jackson' end> Resource = 'artist/{NAME}' Response = RESTResponse1 end
As you can see, the URI ('artist/{NAME}') has an internal parameter, enclosed in curly braces, and this value automatically adds a proper parameter definition to the RESTRequest component. This parameter is of type URL segment. Rather than composing the URL manually, the core URL, the URI and the parameters are automatically combined by the REST client library into the actual HTTP request. To pass a specific value for the search, all you have to do is to replace the default value of the parameter:
procedure TForm4.btnJsonClick(Sender: TObject); var rParam: TRESTRequestParameter; jValue: TJSONValue; begin rParam := RESTRequest1.Params.ParameterByName('NAME'); if Assigned (rParam) then begin rParam.Value := edName.Text; RESTRequest1.Execute; jValue := RESTResponse1.JSONValue; if jValue is TJSONObject then MemoOutput.Text := TJSONObject(jValue).GetValue('artist').ToString else MemoOutput.Text := jValue.ToString; end; end;
Notice that the result is a JSON object I'm processing manually to extract the information I'm looking for. Here is the output of the program:
Again the complete source code is available on my "session demos" repository at code.marcocantu.com/trac/marcocantu_delphi_sessions_demos while the subversions URL for the entire set of demos is at code.marcocantu.com/svn_public/marcocantu_delphi_sessions_demos/. (And this time it should work, sorry for the subversions server misconfiguration when I wrote my previous blog post).