Search code examples
jsonapirestvbscriptasp-classic

How to process the payload send to an API


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.


Solution

  • 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\":\"[email protected]\",\]"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.