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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REST API SAMPLE CODE 1

Status
Not open for further replies.

JINESH GANDHI

IS-IT--Management
May 8, 2022
12
IN
Hello ,

I want to write a code on vfp9 for REST API, I need sample REST API sample code to learn how to write REST API in vfp
 
Well, the main ingredient is making use of HTTP requests, for example by using MS XML:

Code:
loHTTP = CreateObject('Msxml2.ServerXMLHTTP.6.0')
loRequest = loHTTP.open('GET','[URL unfurl="true"]http://classify.oclc.org/classify2/Classify?isbn=9781930919075')[/URL]
loHTTP.send()

DO WHILE loHTTP.readyState<>4
   DOEVENTS 
ENDDO 

? loHTTP.getAllResponseHeaders()
? loHTTP.responseText()

And that's just a glimpse of what you need. What you need to do depends mostly on whether you want to use an API or provide one. And what usually comes before the core usage by GETting some response is authentication, creating an API user account etc. So it strongly depends on what API you want to use.

The core is using the HTTP protocol and that's possible with MS XML, but also with Windows functions in Wininet. When it comes to web it always pays to look what West Wind has to offer. For example West Wind Web Connection:
Chriss
 
Thank You Dear Friend,
I want code for POST and GET method REST API to get authentication key and JSON File.
Please Help me.
 
Well, once you know Msxml2.ServerXMLHTTP you surely find other samples.

POST is done by passing POST instead of GET, obviously, and then the body you post will be the parameter of the loHttp.Send(body) call.

To send JSON the receiver has to understand that what you send is JSON, which is done by sendig an appropriate content/type header. Indeed most such questions are not API specific questions but questions about the HTTP protocol. And they are answered by learning about that protocol. Here's a starting point:


Chriss
 
And the ServerXMLHttp class, if you want to stick to it, also is documented:


And if that's all too much for you and you don't want to learn all of this, then, as initially pointed out, West Wind Web Connection is encapsulating much of it and makes it easier to use APIs without needing to know details about the HTTP protocol. I recommend to learn the detail, but you might not be like me. The overview of the ServerXMLHttp class shows there are not that many properties and methods.

Details are in what to send and how to prepare it. There are topics to cover in cryptography, too.

I don't know how little you know about all those things. It's a lot to cover if you know nothing about APIs, Restful APIs in detail, HTTP, XML, JSON, (parsing or serializing XML and JSON), DOM, Cryptography for encrypting/decrypting/signing data, Character encodings, authentication schemes like OAuth to name just one scheme. It's asked too much to explain this in one thread. So please learn while you go and ask detail questions. It also helps, if we know what API you'd like to use.

Chriss
 
Hi,
I hope following code will help you.

Try
loXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.6.0")

lcVersion = "6"
Catch
loXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
lcVersion = "3"
Endtry
CrLf = CHR(13)+CHR(10)

**API Data send through POST
TEXT TO varQry NOSHOW TEXTMERGE PRETEXT 7 && pretext 15 TEXTMERGE
{"InvoiceNumber":"123","DateTime":"2022-05-01 12:00:00","TotalBillAmount":0.0,"TotalQuantity":0.0,"TotalSaleValue":0.0,"TotalTaxCharged":0.0,"Discount":0.0,"FurtherTax":0.0,"PaymentMode":1,

"InvoiceType":1,"Items":[
{"ItemCode":"IT_1011","ItemName":"Test1","Quantity":1.0,"TaxRate":0.0,"SaleValue":0.0,"TotalAmount":0.0,"TaxCharged":0.0,"Discount":0.0,"FurtherTax":0.0,"InvoiceType":1
},

{"ItemCode":"IT_1012","ItemName":"Test2","Quantity":1.0,"TaxRate":0.0,"SaleValue":0.0,"TotalAmount":0.0,"TaxCharged":0.0,"Discount":0.0,"FurtherTax":0.0,"InvoiceType":1
}
]
}


ENDTEXT

WITH loXMLHTTP
***Production URL
cUrl=" .OPEN("POST", cUrl ,.F.)
.setRequestHeader("Content-Type","application/json; charset=utf-8")
.setRequestHeader("Accept","application/json")
.setRequestHeader("Authorization", "Bearer "+varToken)
.setOption(2, 13056) && ignore all SSL Cert issues
.SEND(varQry)
ResponseWebSite = .responsetext
*MESSAGEBOX(' Response: '+CHR(13)+.responsetext,0)
ENDWITH
 
I absolutely don't discourage examples, it can just help much more, if they're on point about your needs.

I just want to highlight one thing from zaheercmr:
Code:
.setRequestHeader("Authorization", "Bearer "+varToken)

That's part of the HTTP knowledge, to know there are headers of a HTTP request, and specifically some about authentication. What you need will depend on what API you use. A bearer token is not the only thing an Authentication header can have. Also, a bearer token has a history, you get it from a server, usually, after log in, so it is a result of authentication which is used to reidentify you within a session that has a session timeout. A bearer token can also be an access token which doesn't expire.

It's essential to know, again, that not all APIs are the same and what you need depends on how the API is defined, or, if you want to offer an API to others, what you decide to use for it. APIs can be completely without authentication, too. As my ISBN book lookup example shows. There is a lot of public data available via APIs that are free and don't need any authentication. But the majority of APIs do. Google is a good example of changing to complex authentication schemes even though a lot of things remain free to use, like search, maps and many more things.

Alone this topic of getting to the stage where any further request is simpler, can be far easier with standard libraries which usually exist for the typical web languages, like JS or PHP or Python, etc. It's not unusual to shift working on an API to an already existing JS solution and then binding to that from VFP. Just to give you a completely different idea that removes the problem from VFP.

Chriss
 
Hello,

I am not sure whether you want to write a server (you get authentification,.. and deliver data ) or if you want to consume data from a webservice (you send auth,.. and get data)

A good start I think is here :

But of course as mentioned you need details about what you want to deliver or receive.

Regards
tom

Later on, for testing and debugging you may have a look on fiddler , it allows to watch the whole trafic including TLS version, headers, data, ...
 
Just wanted to add a little off-topic from my personal experience, honestly I have never written HTTP clients or servers in VFP, but many webservices in java, c++, python and mostly web clients.
If you struggle too much with Visual Fox Pro or it is too limited remember you can always build what you need in other, more modern, ways.
You can always put your VFP program on top of a more modern client, written in javascript, in easy (but I don't know I'd call them clean) ways.

Personally, server wise, I wouldn't advise building a new server on top of VFP http capabilities, just because it seems a crazy idea in my mind, based on how old it is, but really I have never checked VFP http/s capabilities.


I'm not an experienced VFP developer like the gentlemen (and women) that already answered to you, so take my words with a pinch of salt.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top