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
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
CODE -->
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
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
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
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
RE: REST API SAMPLE CODE
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 just want to highlight one thing from zaheercmr:
CODE
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
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
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.