FoxPro Programming
REST API Call using wwHTTP
Gravatar is a globally recognized avatar based on your email address. REST API Call using wwHTTP
  Kathy
  All
  Jun 7, 2020 @ 02:03pm

Hello there,
I've always been happy with wwHTTP so I usually try the REST API calls in WebSurge and then use wwHTTP to implement them in Foxpro.
I have a new case for which I've been successful with Websurge but not with wwHTTP.
So here is the Websurge request which is successfully done:

And here is the Foxpro code to simulate the call above:

TEXT TO lcEncodedData
	grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=xxxx.xxxx.xxxx
ENDTEXT 
lcURL = "https://xxxxx.com/oauth/token"
DO wwHttp
loHTTP=CREATEOBJECT("wwHttp")
loHttp.cHttpVerb = "POST"
loHttp.cContentType = "application/x-www-form-urlencoded"
loHttp.nHttpPostMode = 4
loHttp.AddPostKey(lcEncodedData)
lcJsonResult = loHTTP.HTTPGet(lcURL)

I would appreciate your help on this,
Kathy

Gravatar is a globally recognized avatar based on your email address. re: REST API Call using wwHTTP
  Rick Strahl
  Kathy
  Jun 7, 2020 @ 03:43pm

Don't set both nHttpPostMode and cContentType - only use one. The post type you want to use in this case is nHttpPostMode = 2 which is predefined and that's all you need. No need to set the cContentType.

nHttpPostMode=4 is raw, meaning you have to specify a content type after - otherwise the content will be application/octet-stream which is a generic type.

More info in the updated docs:

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: REST API Call using wwHTTP
  Kathy
  Rick Strahl
  Jun 8, 2020 @ 10:00am

Thank you so much for the hints and help,
loHttp.nHttpPostMode = 1 works for this case (Not sure if the roles of 1 and 2 are defined as opposite of what they are on the document page).
And apparently another issue for my case was the gap before "grant_type" in the TEXT TO block! And as soon as I removed the spaces, it worked:

TEXT TO lcEncodedData
grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=xxxx.xxxx.xxxx
ENDTEXT 
lcURL = "https://xxxx.com/oauth/token"
DO wwHttp
loHTTP=CREATEOBJECT("wwHttp")
loHttp.cHttpVerb = "POST"
loHttp.nHttpPostMode = 1
loHttp.AddPostKey(lcEncodedData)
lcJsonResult = loHTTP.HTTPGet(lcURL)

Thanks,
Kathy

Gravatar is a globally recognized avatar based on your email address. re: REST API Call using wwHTTP
  Rick Strahl
  Kathy
  Jun 8, 2020 @ 01:36pm

Kathy,

What you're doing works, but the way this is meant to work is the following:

loHttp = CREATEOBJECT("loHttp")

* don't set any content type or post mode (defaults to form data)
loHttp.AddPostKey("grant_type","urn:ietf:params:oauth:grant-type:jwt-bearer")
loHttp.AddPostKey("assertion","xxxx.xxxx.xxxx")

loHttp.Post(lcUrl)

This ensures that the encoding happens correctly for the values of the keys. Unless you have a buffer that's handed to you from elsewhere (like an oAuth cookie or something like that) it's better to use .AddPostKey() so the values get properly encoded.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: REST API Call using wwHTTP
  Kathy
  Rick Strahl
  Jun 8, 2020 @ 03:16pm

Oh, I see.
Thank you so much for correcting and educating me on this.
I can do my calls with more open eyes now.
Best regards,
Kathy

© 1996-2020