0

Before reading on: This is NOT a question about how to access a REST API. I am NOT asking about communicating with the endpoint, sending it a payload, and parsing the received JSON.

Rather, I AM asking about how to set up that endpoint in vbScript. The endpoint itself, not the code that's going to be sending it messages. My post is asking how to receive the JSON payload. I'm editing the question because people just don't really read a post sometimes, and because of that I'm being told I need to defend my post as NOT being the same as some other post which IS NOT THE SAME. Duh. If people aren't going to read, then how do I explain to them?

Anyway, since I made the original post, somebody ACTUALLY did address my question. It is @user692942 below, who wrote (confirming my suspicion) that if I wanted to do this in vbScript, I was going to have to use Request.BinaryRead, and then posted an actual relevant link (which I had missed in my searching) that talked about some of the ways you could use Request.BinaryRead. But for those who want to read my original post:

In vbScript I am trying to write a REST API that is passed a JSON payload (which it then parses, then it does whatever it needs to do, then responds with a resulting JSON string). I've got the output part working, but I realized that I made a fundamental mistake with the input side. I wrote it expecting to receive standard POSTed fields, NOT a JSON string. When I google this problem, all I can find is how to parse the returned JSON from an endpoint, NOT how to create the endpoint api itself, and how it must be written to properly grab the payload.

Thus, I begin the api code with two simple requests:

email = Request.Form("email")
password = Request.Form("userpassword")

Obviously that's wrong, since I'm not receiving fielded data, but instead trying to grab a JSON payload. How do I write this to input the payload properly. (I know how to do the JSON parse, but I can't get the JSON string!) I realize this borders on a dumb question, but could someone please tell me what obvious thing it is that I am missing? Thanks.

user1693404
  • 173
  • 1
  • 12
  • No. It is the OPPOSITE of what I need. I need to create the API itself. I know how to access an api. My problem is writing the api itself (whose job it is to consume the JSON payload passed to it.) – user1693404 Jan 21 '22 at 19:15
  • 1
    You want to write an API in Classic ASP? You are going to be hampered as to process a full body of a request (say a JSON payload) will require use of `Request.BinaryRead()` to read the raw payload as binary and convert it to a serialised string you can parse. Save yourself a load of hassle and build your API in a modern framework that has better support for building APIs. – user692942 Jan 21 '22 at 19:22
  • Does this answer your question? [Unable to get raw POST data when using Classic ASP VBScript](https://stackoverflow.com/a/43244257/692942) – user692942 Jan 21 '22 at 19:26
  • @Dijkgraff, no. That is the opposite of what I need to do. – user1693404 Jan 24 '22 at 05:50
  • 1
    @user692942. Yes, that is relevant, as I explained above, thanking you. – user1693404 Jan 24 '22 at 06:11
  • @user1693404 please enlighten me on how the community "gain points" by down-voting your question? It's actually the opposite it cost -2 points to downvote a question. Also, don't find "Thanks!! (for nothing)" particularly helpful, we directed you to a clear duplicate which you yourself said, "Yes, that is relevant, as I explained above, thanking you." so we did help whether you want to believe it or not. – user692942 Jan 25 '22 at 17:08
  • @user692942 Actually that's not quite correct, but let me hasten to apologize for the snarky comment. Yes, I was shown a relevant earlier post, and I wrote to thank for that. But the message I received at the top of my post, demanding I edit it or face having it restricted, said it was a duplicate of a DIFFERENT post, which WAS NOT relevant, and which was, instead, just another tired, unthinking comment that I was duplicating a request for information RESPONDING to a REST API, not setting one up. Why defend this? It was an example of not taking the time to read what I was asking. – user1693404 Jan 25 '22 at 17:37
  • @user1693404 you have the power to update it if that duplicate wasn't relevant the other certainly was so flag it accordingly and the message at the top of the question will change. The OP has more control over that than anyone. In their defence, your title "How to process the payload send to an API" does not make that clear so I can understand where the confusion could occur. – user692942 Jan 25 '22 at 17:42
  • Look @user692942, you're the person who ultimately helped me out, so I don't want to continue this, since it sounds like an edit-war, but isn't. I appreciate your suggestions. – user1693404 Jan 25 '22 at 17:56
  • @user1693404 There's no malice or offence taken, just want to make sure you have all the facts. I hope you manage to find a solution. – user692942 Jan 25 '22 at 17:57

1 Answers1

1

There is a big distinction between a REST API and a piece of software that merely "CALLS" the REST API. The following is a way to create the REST API itself, using only vbScript. This API can reside at a url ending with myrestapi.asp. When some other progrem calls your "end point" and sends a GET or POST request, you could perform any number of interesting tasks on your server, notably interacting with your MSSQL database, before returning whatever it was to be returned, as a JSON string. (THEN, it would be up to your interlocutor to figure out your answer by parsing that JSON. -- But I stress, THAT task is NOT the focus of this post.)

The following relies on the totally free ASPJSON parser (with Response.LCID = 1033).

It also briefly shows the use of Window's command-line curl to run tests.

One of the problems with vbScript is that it is so old, people assume "api-related" problems must mean you're using vbScript to send a command to an end point and get something back in JSON, and a lot of folks ask about and talk about how to do that.

But it's also possible (as I've recently discovered) to create the endpoints THEMSELVES. The difference is that when you are only ACCESSING an API, you are always dealing with structured data. When you ARE the API, if the data coming in is a JSON string, it is UNstructured. It is not a part of a name-value pair.

But it is very possible, with only a little work, to build such a piece of software using only vbScript. The api reads its Request object, bringing in the raw binary data in a format that can be easily converted to a standard vbscript string. At that point, the JSON parser is called, the result of which is the JSON fields, attached to a vbScript object. The retrieval syntax is very concise. Here's a very simple example login with email and password.

Dim tb,br,i,s,jsonObj,incomingParams,email,password,status
tb = Request.TotalBytes
br = Request.BinaryRead(tb)
s = ""

For i = 1 To tb
    s = s & Chr(AscB(MidB(br, i, 1)))
Next
' s now contains JSON input
Set jsonObj = New JSONobject ' see note above
Set incomingParams = jsonObj.parse(s)
email = incomingParams("useremail")
password = incomingParams("userpassword")
status = checkEmail email,password ' pre-canned verification function
If status = "good" Then
    key = getKey ' precanned routine to get random unique key
    jout = "{""loginstatus"":""" & status & """,""loginkey"":""" & key & """}"
Else
    key = 0
    jout = "{""loginstatus"":""1""}"
End If
Response.ContentType = "application/json"
Response.Write(jout)

And that's it. To try this out, open a Windows cmd window and type:

curl -d "{\useremail\":\"memberemail@gmail.com\",\]"userpassword\":\"rawpassword\"}" -H Content-Type: application/json" https://www.myserver.com/myrestapi.asp

Hit Enter and if you have used an accepted email/password and if you typed the appropriate location for your API endpoint that you're testing, you should see two JSON fields in return, loginstatus, with a 0 (good status) and loginkey with a random sequence, your key.

So now you have your own REST API (of sorts) which CAN be expanded to support a full range of GETs and POSTs. I realize there are a lot of things classic ASP is lacking and that it is often possible to use a more modern language when programming in vbScript. Nevertheless, it's also possible to pull something off in vbScript.

user1693404
  • 173
  • 1
  • 12
  • Keep in mind that you will hit limitations with this approach, mainly decoding the binary when you send a UTF-8 encoded JSON body because using `Chr()` to decode instead of `ChrW()` will lead to problems, but you also won't always have exactly one byte to pull for each character as UTF-8 is a multi-byte encoding. Never said it wasn't possible, just warned against it. There are also other issues like passing the username and password in the body of the request but that's more about how you structure your API and there are plenty of resources on the web about that (Authorization HTTP header). – user692942 Jan 26 '22 at 09:31