Asp.net Rest Service Help

Hi

Hope someone can help, I have got to try and consume a Rest service details here : https://developer.gocardless.com/pro/#integration-guide-collecting-payments
I have had some success using Delphi XE7 for the desktop side, but our website needs some integration
which I am trying to do using Oxygene, We need to be able to setup the initial customers/Bank accounts etc online

I have got a SSL cert setup, The Rest Api uses Basic Authentication then uses calls like

POST https://api.gocardless.com/customers HTTP/1.1
{
“customers”: {
“email”: "user@example.com",
“given_name”: “Frank”,
“family_name”: “Osborne”,
“address_line1”: “27 Acer Road”,
“address_line2”: “Apt 2”,
“city”: “London”,
“postal_code”: “E8 3GX”,
“country_code”: “GB”,
“metadata”: {
“salesforce_id”: “ABCD1234”
}
}
}

I don’t know where to start, some help with how I do the basic auth in oxygene and get me started calling the various methods would be great.

You should use the httpclient nuget package

https://www.nuget.org/packages/Microsoft.Net.Http and then the code is something like

var client := new HttpClient;

  client.DefaultRequestHeaders.Authorization := new AuthenticationHeaderValue('basic','username:password');
  client.PostAsync(url,new StringContent(jsonText, Encoding.ASCII, 'application/json'));

You probably also need the json.net nuget package to serialize/deserialize json objects.

Cheers,
John

Hi John

Thank you for replying. I have installed the 2 packages you mentioned and im attempting to make a GET work but cant seem to get any response? not even an error back. (I have removed my credentials)

This is what i’m trying -

var
GoCardlessUrl :String;
response: HttpResponseMessage;
begin
var client := new HttpClient;
GoCardlessUrl := ‘https://api-sandbox.gocardless.com/customers’;
client.DefaultRequestHeaders.Authorization := new AuthenticationHeaderValue(‘basic’,‘username:password’’’);
client.DefaultRequestHeaders.Add(‘GoCardless-Version’,‘2014-11-03’);
response := await client.GetAsync(GoCardlessUrl);
Label1.Text:= response.Content.ToString;

Any Ideas
Regards

David

What kind of application are you testing from ?

You can make it synchronous, its easier to test that way

var getTask := client.GetAsync(url);
getTask.Wait;
getTask.result.StatusCode

StatusCode gives you the HttpStatusCode

or you can do something like this getTask.Result.IsSuccessStatusCode

Hi John

Its an Asp.net website,

The https://api-sandbox.gocardless.com/customers is meant to return Json or an error also in json

what is getTask? a HttpResponseMessage!!

Regards

David

The get is asynchronous. GetAsync returns a Task, by calling Wait it will wait for the get to finish. Task.Result is then a HttpResponseMessage.

If your developing an asp.net website, I think you want to always wait for the async operations to finish rather than using await.

If you wait you can return proper responses from the calls to your website.

http://docs.elementscompiler.com/Oxygene/Expressions/Await/#q=await

John

Thank you for your help so far, you have helped no end.

I can now receive results that I get back in Json.
Do you happen to know the best way to Deserialize json when you don’t know the format you will get back.

im trying
var jsonString := PostTask.result.content.ReadAsStringAsync();
var jsonObj := JsonConvert.DeserializeObject(jsonString);
do you know how you search for key/values on the jsonObj?

Thank you very much

David

I use ExpandoObject

var stuff : dynamic := JsonConvert.DeserialzeObject<ExpandoObject>(jsonString);

if you want to search you can also cast that as IDictionary<String,Object>