×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

REST API SAMPLE CODE

REST API SAMPLE CODE

REST API SAMPLE CODE

(OP)
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

RE: REST API SAMPLE CODE

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','http://classify.oclc.org/classify2/Classify?isbn=9781930919075')
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: https://webconnection.west-wind.com/docs/_4iu1ev8p...

Chriss

RE: REST API SAMPLE CODE

(OP)
Thank You Dear Friend,
I want code for POST and GET method REST API to get authentication key and JSON File.
Please Help me.

RE: REST API SAMPLE CODE

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:

https://developer.mozilla.org/en-US/docs/Web/HTTP/...

Chriss

RE: REST API SAMPLE CODE

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

https://docs.microsoft.com/en-us/previous-versions...)

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

RE: REST API SAMPLE CODE

(OP)
Thank you chris miller.

RE: REST API SAMPLE CODE

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="https://YourDestinationWebsite/api/Live/PostData"
.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

RE: REST API SAMPLE CODE

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

RE: REST API SAMPLE CODE

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 :
https://west-wind.com/wconnect/weblog/ShowEntry.bl...
https://webconnection.west-wind.com/docs/_4iu1ev8p...

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, ...

RE: REST API SAMPLE CODE

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close