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!

HTTP POST parameters

Status
Not open for further replies.

Richard Guelzow

Programmer
Jan 6, 2022
22
0
1
US
Hi All,

I am trying to format this example for VFP:

URL: BODY: {
"ClinicID": "134727",
"UserName": "PIMS_USER",
"Password": "devtest"
}

I believe that the parameters are passed as part of the SEND and this is what I have so far. When I run this, I receive an Error 404 file/directory not found. Any thoughts on how to format the SEND? Thank you in advance. Richard

Code:
PUBLIC oHTTP
m.oHTTP = CREATEOBJECT('Microsoft.XMLHTTP')

m.oHTTP.OPEN("POST", '[URL unfurl="true"]https://onlineapi-onboard.antechdiagnostics.com/v1.1/Users/login',.F.)[/URL]
m.oHTTP.SEND("ClinicID=134727?UserName=PIMS_USER?Password=devtest")
m.cResult = oHTTP.responseText
m.cStatus = m.oHTTP.STATUS

? m.cResult
? m.cStatus
 
Hello,

the api seems to expect JSON format, so you have to send a string like the sample
lCsend = 'BODY: {"ClinicID": "134727","UserName": "PIMS_USER","Password": "devtest"}'
to my opinion lCsend = '{"ClinicID": "134727","UserName": "PIMS_USER","Password": "devtest"}'

For Json there are good lib in vfpx : and others like this one with functions to send :

Remarks
You may use Createobject("Msxml2.ServerXMLHTTP.6.0") or better (usually) use chilkat (commercial)

Regards
tom
 
You're given the send body, why do you change it?

Also, do you have any more developer reference to this API?
We can only do guesswork here without knowing the details.

It seems to be JSON, so better set the requests Content-Type header with oHTTP.setRequestHeader("Content-Type", "application/json")

Overall:
Code:
LOCAL loHTTP, lcBody, lcResult, lnStatus, lcStatus
loHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")

Text To lcBody NoShow
{
"ClinicID": "134727",
"UserName": "PIMS_USER",
"Password": "devtest"
}
EndText

m.loHTTP.setRequestHeader("Content-Type", "application/json")
m.loHTTP.open("POST", '[URL unfurl="true"]https://onlineapi-onboard.antechdiagnostics.com/v1.1/Users/login',.F.)[/URL]
m.loHTTP.send(lcBody)
Do While m.loHTTP.readyState<>4
   Doevents
EndDo
lcResult = m.loHTTP.responseText
lnStatus = m.loHttp.status
lcStatus = m.loHTTP.statusText

You should also find out the codepage or charset used by the API because it could become important to get the password over correctly. VFP "speaks" the ANSI codepage that usually translates to charset Latin1, and that has an overlap with UTF8, but some characters may need conversion. Not unlikely, as a password generally should also contain special characters, not only letters and digits.

Chriss
 
webapidocs_ulbbvb.png


I can only hope since you're in the "inner circle" of clients using that API, you got more than that.
It's a disgrace, so far, from the side of the API provider.

Chriss
 
Chriss is giving you the right code for the request. I would also recommend using MSXML2.ServerXMLHTTP.6.0 or even better Chilkat ActiveX, like Tom suggested, if things get more complicated in the future like sending files, using certificates for authentication etc.

What you initially tried to do was creating query parameters, however, you would have to add query parameters directly to the URL like this:

m.oHTTP.OPEN("POST", '
Query parameters are more often used with GET requests, because with GET you can't send a body. However, you were given a JSON example and the correct way to send a JSON string is to put it in the body of your POST request like Chriss showed you.

The third way to add information to your REST requests (POST/GET/PUT/DELETE) is to put them in the header. Usually you put in the header the format you are sending ("Content-Type","application/json") and the format you are accepting ("accept", "application/json"). Sometimes the API wants you to send an authorization bearer or a username and password in the header, so you add them

loHTTP.setRequestHeader("Content-Type","application/json")
loHTTP.setRequestHeader("Accept", "application/json")
loHTTP.setRequestHeader("Authorization", "Bearer 5sd3j3hH35Kh3hlkH")
loHTTP.setRequestHeader("Username", "user123")
loHTTP.setRequestHeader("Password", "password123")

Most of the time the response you receive is in JSON format and you simply parse the body of the response (loHTTP.responseText).

Seldomly the response can also contain information in the response header, i.e. an authorization bearer and you can get to that information for example by looking inside

loHTTP.getResponseHeader("Authorization")

Regards,
Manni
 
Good morning all - yes you are all correct - this API is expecting JSON. I do have some documentation and a help desk to support me, but they have no knowledge of VFP. I will give your ideas a whirl and see if I can get this to work and report back. Thanks again for your help.

Richard
 
all good additional advice, ResponseText and the status isn't everything you receive in the response.
Let me point out there's also m.loHTTP.getAllResponseHeaders()

More general intellisense is your friend besides online documentation, not only MS, about MSXML2.ServerXMLHTTP.6.0 and other XMLHttpRequest objects available.

I also looked for the API in the general antechdiagnostics.com homepage without finding something. At least you got more as you got that example body to send. When you use POST you usually don't send query parameters within the query URI, you send a request body, that's the parameter of the send method, which usually is empty when doing a GET request. And there's more than GET and POST, an API usually depends on these and then also PUT, DELETE and there are more, see a good overall reference like
It pays to learn about the HTTP protocol, as there are many things involved not visible at first sight like the request and response headers. And also about the process itself: One thing you didn't do in your code was waiting for the readyStatus. when you do things manually in the command window, the response likely will already be there soon enough after you sent the request, but within a PRG you can easily fail to receive a response, simply because you don't wait for it. This is a really very basic concept in any HTTP communication protocol. And that's all independent of VFP, ie automating a HTTPRequest class is done in the same manner in VFP or VB6, VBA, VB.NET or C# dotNET in general and ASP.NET just have their own framework of new libraries, but they also use the same protocol and processes, that's HTTP, not any programming language. If you don't have this and further background knowledge you can easily step on your own feet in many ways.

Chriss
 
Chris - I was able to use your code and it is working perfectly. I also found that I had a typo in the url as well. Thanks to all for your assistance & advice. Much appreciated.

Richard

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top