Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VFP 9 To Access The Parse.Com Restful API

Status
Not open for further replies.

AnilKChadha

Programmer
Oct 20, 2014
8
GB
Hi,


Does anyone in the community has any knowledge on using VFP 9 to access the Parse.Com Restful API?

e.g Here's a CURL script

curl -X POST -H "X-Parse-Application-Id: rEQZDWCoN3MbhOQwSZMMU2MgDKWYp5AQSRlRZJ2H" -H "X-Parse-REST-API-Key: 9pnEkZ93SuZBQfyHRBW735whEwOvUkS3QU1tfoXo" -H "Content-Type: application/json" -d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}'
and here's a Python script

import json,httplib
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
connection.request('POST', '/1/classes/GameScore', json.dumps({
"score": 1337,
"playerName": "Sean Plott",
"cheatMode": False
}), {
"X-Parse-Application-Id": "rEQZDWCoN3MbhOQwSZMMU2MgDKWYp5AQSRlRZJ2H",
"X-Parse-REST-API-Key": "9pnEkZ93SuZBQfyHRBW735whEwOvUkS3QU1tfoXo",
"Content-Type": "application/json"
})
results = json.loads(connection.getresponse().read())
print results

Thanks in advance
 
check out wwHTTP from west-wind.

you can do loHTTP.AddPostKey() and loHTTP.HttpGet() etc..

Ez Logic
Michigan
 
You can also use WININET API or MSXML2.XMLHttp

Code:
loRequest = Createobject('MsXml2.XmlHttp')
ComProp(loRequest,'UTF8',1)
loRequest.SetRequestHeader('X-Parse-Application-Id','...yourid...')
loRequest.SetRequestHeader('X-Parse-REST-API-Key','...yourkey...')
loRequest.SetRequestHeader('Content-Type','application/json')

Text To lcBody Noshow
  {"score":1337,"playerName":"Sean Plott","cheatMode":false}
Endtext

loRequest.Open("POST", "[URL unfurl="true"]https://api.parse.com/1/classes/GameScore")[/URL]
loRequest.Send(lcBody)
Do While loRequest.ReadyState <> 4
   Doevents Force
Enddo
? loRequest.ResponseBody

Bye, Olaf.
 
And by the way: You don't post secrets like your API ID and Key to public forums.

Bye, Olaf.
 
Hi Olaf,


Thanks for the reply - I will check out your sample code and run it shortly.

BTW, The Parse APP ID and Key is actually directly off the Parse.Com examples and so are probably not genuine keys.


Anil
 
OK,

Here's a little fix, yuo can't setrequest headers before opening a request, so commands have to go in this order:

Code:
loRequest = Createobject('MsXml2.XmlHttp')
ComProp(loRequest,'UTF8',1)

Text To lcBody Noshow
  {"score":1337,"playerName":"Sean Plott","cheatMode":false}
Endtext

loRequest.Open("POST", "[URL unfurl="true"]https://api.parse.com/1/classes/GameScore")[/URL]
loRequest.SetRequestHeader('X-Parse-Application-Id','...yourid...')
loRequest.SetRequestHeader('X-Parse-REST-API-Key','...yourkey...')
loRequest.SetRequestHeader('Content-Type','application/json')
loRequest.Send(lcBody)
Do While loRequest.ReadyState <> 4
   Doevents Force
Enddo
? ""+loRequest.ResponseBody

Also in my test the response body comes back as varbinary value and the easiest way to convert to readable text is ""+vabinary.

Bye, Olaf.
 
Next step: GET the POSTed object.

Code:
loRequest.Open("GET", "[URL unfurl="true"]https://api.parse.com/1/classes/GameScore/...id[/URL] which came back from POST...")
loRequest.SetRequestHeader('X-Parse-Application-Id','...yourid...')
loRequest.SetRequestHeader('X-Parse-REST-API-Key','...yourkey...')
loRequest.SetRequestHeader('Content-Type','application/json')
loRequest.Send(.NULL.)
Do While loRequest.ReadyState <> 4
   Doevents Force
Enddo
? ""+loRequest.ResponseBody

Bye, Olaf.
 
Hi Olaf,


Got the POST and the GET working as per your code - thanks.

However, the GET doesn't work correctly in that the result seems to be buffered. i.e. The first tine I issue the GET then the ResponseBody has the correct data. If i then go to the Parse Back end and manually update the data and run the GET code again, it does NOT show me the updated data but still has the data as per the first GET call. If I exit Foxpro and run the GET code then it does have the updated data.

Is there any way to refresh the buffered data in the HTTP request? I am assuming there would be some parameter in the HTTP request itself.


Thanks

Anil

 
MsXml2.XmlHttp is hooked into the Internet Explorer environment including internet settings like proxy and caching, so look into your IE settings.
Besides that caching can happen server side, too. The way to get more control is via headers, not via a parameter. If so, ask Parsee.com staff, I wasn't even aware of them since you mentioned this service.

Bye, Olaf.
 
Hi Olaf,


There definately isn't any cacheing done server side as my CURL/PYTHON/LUA scripts all work fine after I manually edit the data server side. It is purely the Foxpro GET that is caching.

I had a very quick look at your link and think I would need to set : Cache-Control: max-age=0, no-cache, no-store

How would I achieve this in the headers in your sample code?


I have been able to disable the caching manually by the following :

Go to Internet Options. On the General tab, under Browsing History click Settings. Select the "Every time I visit the webpage" radio button.

This doesn't "disable" the cache per se, but it works but I would rather have the programmatic fix as I can not guarantee that the internet options on the client pc will be set accordingly.


Thanks
 
Well,

you have understood the resources quie well, what hinders you seeing the SetRequestHeader call needeed? It works the same way as with the other three headers.

Bye, Olaf.
 
Hi Olaf,


I turned off the IE manual settings that I had changed to originally 'fix' the cache issue and tried the following code but it did not work :

loRequest.SetRequestHeader('Cache-Control','max-age=0')
loRequest.SetRequestHeader('Cache-Control','no-cache')
loRequest.SetRequestHeader('Cache-Control','no-store')


or


loRequest.SetRequestHeader('Cache-Control','max-age=0,no-cache,no-store')

Any ideas?
 
Using Olaf's code, you just need to set the async parameter to .f.,

Try:

Code:
CLEAR

TEXT To lcBody NOSHOW PRETEXT 15
{
"score":1337,
"playerName":"Sean Plott",
"cheatMode":false
}
ENDTEXT

appId  = 'rEQZDWCoN3MbhOQwSZMMU2MgDKWYp5AQSRlRZJ2H'
apiKey = '9pnEkZ93SuZBQfyHRBW735whEwOvUkS3QU1tfoXo'

lorequest = Createobject('MsXml2.XmlHttp')

With lorequest As msxml2.xmlhttp

	* check for async parameter, it defaults to T
	.Open("POST", "[URL unfurl="true"]https://api.parse.com/1/classes/GameScore",.F.)[/URL]
	.setrequestheader('X-Parse-Application-Id',m.appId)
	.setrequestheader('X-Parse-REST-API-Key',m.apiKey)
	.setrequestheader('Content-Type','application/json')

	.Send(lcbody)

	? lorequest.responseText

Endwith
 
Cache-Conttrol is 1 header, not three. You have to provide the value line in one SetRequestHeader.

Bye, Olaf.
 
Olaf,


I did try it on 1 line i.e. loRequest.SetRequestHeader('Cache-Control','max-age=0,no-cache,no-store') but it was still cached.


Anil
 
You can:

-Use Msxml2.ServerXMLHTTP instead of msxml2.xmlhttp

-Try appending a random parameter string at the end of your request:

Code:
loRequest.Open("GET", "[URL unfurl="true"]https://api.parse.com/1/classes/GameScore/"+yourKey+"?random="+sys(2015),.f.)[/URL]
 
Anil, I can't help you with something I haven't yet achieved. Study cache options in more detail. There's more to it than that one header you could do.

Adding a random parameter, which is not needed and evaluated by PArse.COM makes each request a unique request, give it a try. The API has to play nice with this and not return an error due to the unneeded and unwanted parameter passed in.

Bye, Olaf.
 
Hi mplaza,


That random parameter worked!!!! Didn't need to use Msxml2.ServerXMLHTTP.

BTW, is there much difference in Msxml2.ServerXMLHTTP against Msxml2.XMLHTTP?


Thanks so much

and thanks to Olaf too with all the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top