West Wind Internet and Client Tools
Create a request object in VFP using WWCT
Gravatar is a globally recognized avatar based on your email address. Create a request object in VFP using WWCT
  Dmitry Litvak
  All
  Dec 21, 2016 @ 07:50am

Hi,

Anybody could suggest if the following C# code can be done in VFP (using West Wind Client Tools):

var request = new RestRequest("/token", Method.POST);

request.AddParameter("grant_type", "password");
request.AddParameter("username", textBoxUserName.Text);    
request.AddParameter("password", textBoxPassword.Text);
            
var response = _client.Execute<AuthResponse>(request);

How could I create the http request object (I think the var request is http object) in VFP 9 and WWCT?

TIA

Gravatar is a globally recognized avatar based on your email address. re: Create a request object in VFP using WWCT
  Rick Strahl
  Dmitry Litvak
  Dec 21, 2016 @ 11:18am

Sure, although you're not showing your entire request data that's being set up so I'm filling in the blanks here:

loHttp = CREATEOBJECT("wwHttp")

*** Add headers
loHttp.AddHeader("grant_type","password")
loHttp.AddHeader("username", textBoxUserName.Text);    
loHttp.AddHeader("password", textBoxPassword.Text);

loHttp.ContentType = "application/json"
loHttp.AddHeader("Accept","application/json")

*** Some way to serialize JSON
loSer = CREATEOBJECT("wwJsonSerializer")
lcJson = loSer.Serialize(loFoxObject)
loHttp.AddPostKey(lcJson)

*** Send the request
lcJsonResult = loHttp.HttpGet(lcUrl)

loResult = loSer.DeserializeJson(lcJsonResult)

But - if you're doing the above you'd be much better off using the wwJsonServiceClient which automates the JSON serialization/deserialization steps.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Create a request object in VFP using WWCT
  Dmitry Litvak
  Rick Strahl
  Dec 21, 2016 @ 11:50am

UPDATE 2: I resolved this and I am getting back from CallService the key that I need.

Thank you for your reply and the suggested code.

I was actually trying to use the sample of calling JSON REST from your article: http://west-wind.com/wconnect/weblog/ShowEntry.blog?id=920

But I need to execute POST "http://www.mysite.com/webapi/token" url with the body having the following format: username=5000&grant_type=password&password=ABC123 (I am quoting the author of the Web API)

What goes over my head is the term "body having the format". I thought I had to use this code:

loReturn = loProxy.CallService("http://mysite.com/webapi/token","","POST")

Where the second parameter (above empty string "") containing "the body having user name and password". But I don't understand how.

If you see what I am missing, please let me know. Thank you in advance.

UPDATE:. I tried to change the above loProxy expression to the following (just guessing is this what has to be done):

loReturn = loProxy.CallService("http://mysite.com/webapi/token","username=5000&grant_type=password&password=ABC123","POST")

But the loReturn is NULL and loProxy.cErrorMsg has "Insternal Server Error"

UPDATE 2. I also tried to the change the above code using the example in your article of Posting data to server.

So I created an empty object and added three parameters.

Then I have this call:

loUser = CREATEOBJECT("Empty")
ADDPROPERTY(loUser,"grant_type","password")
ADDPROPERTY(loUser,"username","1233")
ADDPROPERTY(loUser,"password","XYX")
loReturn = loProxy.CallService("http://mysite.com/webapi/token",loUser,"PUT")

But the loProxy.cErrorMsg is "Bad Request"

I tried the above call changing "PUT" with "POST" but to no avail.

Again, please let me know if you see what I am doing wrong.

Gravatar is a globally recognized avatar based on your email address. re: Create a request object in VFP using WWCT
  Rick Strahl
  Dmitry Litvak
  Dec 21, 2016 @ 06:40pm

The data you are talking about is URL encoded. So these are not headers, but actual content values.

With wwHttp you'd use:

loHttp = CREATEOBJECT("wwhttp")

*** not necessary - automatic because of POST data
* loHttp.cHttpVerb = "POST"

loHttp.AddPostKey("grant_type", "password");
loHttp.AddPostKey("username", textBoxUserName.Text);    
loHttp.AddPostKey("password", textBoxPassword.Text);

lcResult = loHttp.HttpGet("http://www.mysite.com/webapi/token")
Gravatar is a globally recognized avatar based on your email address. re: Create a request object in VFP using WWCT
  Dmitry Litvak
  Rick Strahl
  Dec 21, 2016 @ 06:43pm

Yes, of course, you are right. Thank you!

© 1996-2024