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 SkipVought 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
0
0
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
 
Good it worked..

From :

"XMLHTTP is designed for client applications and relies on URLMon, which is built upon Microsoft Win32 Internet (WinInet). ServerXMLHTTP is designed for server applications and relies on a new HTTP client stack, WinHTTP. ServerXMLHTTP offers reliability and security and is server-safe."

That's why your IE config is tied to your requests when you use XMLHTTP.

There's too many considerations, but mostly related to serve programming ( If you need to get/post from asp pages for example, you must use serverHttp ).
 
Msxml2.ServerXMLHTTP vs Msxml2.XMLHTTP...
this is what I found:
Since your app runs on a client and VFP is Win32 API like WinInet you should rather use Msxml2.XMLHTTP than Msxml2.ServerXMLHTTP.

Even if you don't have the time you must take the time to understand, how things work. Knowledge takes time.

Guide to HTTP Caching said:
the browser itself also maintains an aggressive cache, which keeps clients from having to continually ask your server for a resource each time it comes up.

This is one thing hindering any cache control headers to take effect instead of browser settings.
The cache headers are actually for the server side of responses, not for the client side. Take a look via ? loRequest.GetAllResponseHeaders() as last line
Parse.com isn't sending a cache-control header, so responses are cached - not only by your browser.

Bye, Olaf.
 
In regard to some nice cache behaviour Parse.Com Staff should add ETag to their responses, then the need to attach a random value to each request would not exist and you could make use of the cached responses in case the stored object really didn't changed.


Bye, Olaf.
 
Hello Everyone,

I am glad that somenbody is bringing up the topic of Parse.Com as funnily enough I am looking at using the product myself as it is free and owned by Facebook.

I have been able to get as far as the OP now has but was not able to successfully incorporate the where clause in a Parse GET.

Please find the CURL sample code below

curl -X GET -H "X-Parse-Application-Id: rEQZDWCoN3MbhOQwSZMMU2MgDKWYp5AQSRlRZJ2H" -H "X-Parse-REST-API-Key: 9pnEkZ93SuZBQfyHRBW735whEwOvUkS3QU1tfoXo" -G --data-urlencode 'where={"playerName":"Sean Plott","cheatMode":false,"createdAt":{"$gte":{"__type":"Date","iso":"2011-08-21T18:02:52.249Z"}}}' --data-urlencode 'order=score,-name' --data-urlencode 'limit=200'
and the Python sample code

import json,httplib,urllib
connection = httplib.HTTPSConnection('api.parse.com', 443)
params = urllib.urlencode({
"where":json.dumps({
"playerName": {
"$nin": [
"Jonathan Walsh",
"Dario Wunsch",
"Shawn Simon"
]
}
}),
"order":"score,-name",
"limit":200})
connection.connect()
connection.request('GET', '/1/classes/GameScore?%s' % params, '', {
"X-Parse-Application-Id": "rEQZDWCoN3MbhOQwSZMMU2MgDKWYp5AQSRlRZJ2H",
"X-Parse-REST-API-Key": "9pnEkZ93SuZBQfyHRBW735whEwOvUkS3QU1tfoXo"
})
result = json.loads(connection.getresponse().read())
print result

I would be extremely grateful if anyone could help as Parse looks like an ideal cloud data storage solution.

Gill
 
Well, what this does is add parameters to the URL.

For example this queries objects with playerName "Sean Plott":
Code:
loRequest.Open('GET', ;
'[URL unfurl="true"]https://api.parse.com/1/classes/GameScore/?where={"playerName":"Sean[/URL] Plott"}')

The URL can get "messy", well, lengthy at least. And if you want to put in some UTF-8 characters or even some ANSI or ASCII chars not working in URLs, these need to be URL encoded.

Bye, Olaf.
 
You're fighting on several fronts, if you don't even know http requests.

In general the URL of a request with parameters is this way:

Code:
http(s)://baseURL/?parm1=value1&parm2=value2&parm3=value3

Values can be put into quotes to make clearer they are string values, but more important the ? begins parameterization and therefore isn't allowed in any other place in the URL, the & separates parameters and so isn't allowed in values or parameter names. The allowed charset is very narrow, letters and numbers and some more characters, even spaces should rather not occur, but be replaced with %20 or in short a + sign. This also already tells the general way to encode any not allowed char via % and the hexnumber, eg space is ASCII code 32 = 20 in hex (2*16=32). Also when you want a % in a value you have to encode it as %25, & is %26 and ? is %3f. There is --data-urlencode in curl for this and you might use libcurl for VFP: instead of using MsXml2.XmlHttp or Msxml2.ServerXMLHTTP.

Bye, Olaf.

PS: There is an API function InternetCanonicalizeUrl you can also use for urlencoding, but using libcurl you can make use of the libcurl samples.

PPS: The & sign (ampersand) is appearing as paragraph sign in my short URL code example. Makes we wonder, what's wrong with the forum.

PPPS: Ah I see & para means paragraph here. TGML in action, so I replaced with parm1,2,3...
 
I have my own small routine, mostly to fix swedish diacritical letters, something like this:
Code:
FUNCTION FixUrl
PARAMETERS url
IF TYPE('url')='C' AND LEN(url)>0
	url=STRTRAN(url,' ','%20')
	url=STRTRAN(url,',','%2C')
	url=STRTRAN(url,':','%3A')
	url=STRTRAN(url,'å','%E5')
	url=STRTRAN(url,'ä','%E4')
	url=STRTRAN(url,'ö','%F6')
	url=STRTRAN(url,'é','%E9')
	url=STRTRAN(url,'ü','%FC')
	RETURN url
ELSE
	RETURN ''
ENDIF
 
Here I post a url encoder, decoder and parameter encoder / decoder class based on js.
Also a test example sending a request to a echo server.

Marco Plaza
@vfp2nofox


Code:
CLEAR

*
* for testing purposes, we want to see what the server receives;
* [URL unfurl="true"]www.httpbin.org[/URL] is a service for that matter, it will echo the 
* request you send, in this case we'll use the get service.
*
* to see it in action, simply test:
* [URL unfurl="true"]http://httpbin.org/get?sendPar1=a[/URL] value&sendPar2=other value&todayIs=05/08/2015
*
* Visit site for full options.
*
	site = '[URL unfurl="true"]http://httpbin.org/get'[/URL]

* this function creates a url encoded parameter list

	params = "" 
	addparameter(@params,[ where ]         , [{"playerName":"Sean Plott"}])
	addParameter(@params,[order]           , [ score,-name ])
	addparameter(@params,[aTrickyparameter], [[URL unfurl="true"]http://www.test.com/default.html?tool=VFP9+sp2[/URL]])

	m.url = m.site+m.params

* values for request header:
appid  = 'rEQZDWCoN3MbhOQwSZMMU2MgDKWYp5AQSRlRZJ2H'
apikey = '9pnEkZ93SuZBQfyHRBW735whEwOvUkS3QU1tfoXo'

lorequest = Createobject('Msxml2.ServerXMLHTTP')

With lorequest As msxml2.ServerXMLHTTP

	.Open("GET", m.url,.F.)

	.setrequestheader('X-Parse-Application-Id',m.appid)
	.setrequestheader('X-Parse-REST-API-Key',m.apikey)

	.setrequestheader('Content-Type','application/json') 
	.Send()

* remember site will return a json response string ,
* so your json parameter will show with escaped characters - example "  will show as  \" 

	MESSAGEBOX( .responsetext,0)

Endwith


*---------------------------------------------
FUNCTION addParameter(urlParams,name,value)
*---------------------------------------------

oJs = CREATEOBJECT('js')

urlParams = m.urlParams +IIF(m.urlParams='?','&','?')+ALLTRIM(name)+'='+ oJs.encodeuricomponent(TRANSFORM(value))


********************************
Define Class js As Custom
********************************
	ojs = ""

*---------------------------
	Procedure Init
*---------------------------
	With This
		.ojs=Createobject('MSScriptcontrol.scriptcontrol.1')
		.ojs.Language =[JavaScript]
	Endwith

*------------------------------------------
	Function Escape(lccadena As String ) As String
*------------------------------------------
	Return This.ojs.Run('encodeURI',lccadena)

*------------------------------------------
	Function encodeuri(lccadena As String ) As String
*------------------------------------------
	Return This.ojs.Run('encodeURI',lccadena)

*--------------------------------------------
	Function decodeuricomponent(lccadena As String ) As String
*--------------------------------------------
	Return This.ojs.Run('decodeURIComponent',lccadena)

*------------------------------------------
	Function encodeuricomponent(lccadena As String ) As String
*------------------------------------------
	Return This.ojs.Run('encodeURIComponent',lccadena)

*------------------------------------------
	Function unescape(lccadena As String ) As String
*------------------------------------------
	Return This.ojs.Run('decodeURI',lccadena)

*------------------------------------------
	Function decodeuri(lccadena As String ) As String
*------------------------------------------
	Return This.ojs.Run('decodeURI',lccadena)

*---------------------------------
Enddefine
*---------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top