Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a request's body using classic ASP?

How do I access what has been posted by a client to my classic ASP server? I know that there is the Request.Forms variable, but the client's request was not made using a Form. The client request's body is just a string made using a standard POST statement. Thanks

like image 295
user798719 Avatar asked Mar 19 '12 18:03

user798719


3 Answers

You need to read request bytes if content type of request sent by client is not form data. In this case, request is not a form-data that is accessible through name-value pairs so you cannot use Request.Form collection. I suggest investigate the BinaryRead method.

Reading posted data and convert into string :

If Request.TotalBytes > 0 Then
    Dim lngBytesCount
        lngBytesCount = Request.TotalBytes
    Response.Write BytesToStr(Request.BinaryRead(lngBytesCount))
End If

Function BytesToStr(bytes)
    Dim Stream
    Set Stream = Server.CreateObject("Adodb.Stream")
        Stream.Type = 1 'adTypeBinary
        Stream.Open
        Stream.Write bytes
        Stream.Position = 0
        Stream.Type = 2 'adTypeText
        Stream.Charset = "iso-8859-1"
        BytesToStr = Stream.ReadText
        Stream.Close
    Set Stream = Nothing
End Function

Hope it helps.

Update #1:

With using JScript

if(Request.TotalBytes > 0){
    var lngBytesCount = Request.TotalBytes
    Response.Write(BytesToStr(Request.BinaryRead(lngBytesCount)))
}

function BytesToStr(bytes){
    var stream = Server.CreateObject("Adodb.Stream")
        stream.type = 1
        stream.open
        stream.write(bytes)
        stream.position = 0
        stream.type = 2
        stream.charset = "iso-8859-1"
    var sOut = stream.readtext()
        stream.close
    return sOut
}
like image 70
Kul-Tigin Avatar answered Nov 02 '22 11:11

Kul-Tigin


To get the JSON string value just use CStr(Request.Form)

Works a treat.

like image 20
Iain Wood Avatar answered Nov 02 '22 11:11

Iain Wood


In Classic ASP, Request.Form is the collection used for any data sent via POST.

For the sake of completeness, I'll add that Request.QueryString is the collection used for any data sent via GET/the Query String.

I would guess based on the above that even though the client is not a web browser, the Request.Form collection should be populated.


note: all of this is assuming the data being sent is textual in nature, and that there are no binary uploads (e.g. pictures or files) being sent. Update your question body if this is an incorrect assumption.


To test, write out the raw form data and see what you have - something along the lines of:

Response.Write(Request.Form)

Which with a regular web page will output something like

field=value&field2=value2

If you get something along those lines, you could then use that as a reference for a proper index.

If you do not get something like that, update your question with what you tried and what you got.

like image 1
AnonJr Avatar answered Nov 02 '22 10:11

AnonJr