10

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

user692942
  • 16,398
  • 7
  • 76
  • 175
user798719
  • 9,619
  • 25
  • 84
  • 123
  • 1
    I'd like to give you a more complete answer, and maybe clear up what may be a misconception, but first I'd need to know what you mean by "just a string made using a standard POST statement"... especially since if it is a web page, the browser fills in a `
    ` for you if you didn't explicitly put one in your code.
    – AnonJr Mar 19 '12 at 18:57
  • Updated answer now that I know we are dealing with an iOS client. Updating your question with some of what you've tried would help @Kul-Tigin and I answer your question. – AnonJr Mar 20 '12 at 23:09

3 Answers3

26

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
}
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • Thank you very much. I knew something was missing from my understanding. I will investigate your solution. Appreciate it. – user798719 Mar 19 '12 at 20:15
  • Hi, I could not get your solution to work. Could you or someone show me how this is done in jscript? The hang up is in Stream.Write bytes. The interpreter indicates that it is expecting a ";". I tried mixing and matching jscript and vbscript on the same page but didn't get very far. – user798719 Mar 20 '12 at 18:49
  • Thanks so much for taking the time to help a total stranger. You are great! (and your solution works) – user798719 Mar 21 '12 at 04:07
  • Hey, soz for bumping old thread. Can you please point for me where to call the JSON file in the `Request`? I have an external JSON file called `jsonexample.json`. How do I make this file as an input? – emen Sep 10 '13 at 02:58
  • @amnbhrm it's another question's subject, is not related with this question. Please feel free to ask a new question, I'd be glad to help if I could. – Kul-Tigin Sep 11 '13 at 23:18
2

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

Works a treat.

fancyPants
  • 50,732
  • 33
  • 89
  • 96
Iain Wood
  • 21
  • 1
  • This is even documented [here](https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms525985%28v%3dvs.90%29#remarks): If your application requires unparsed data from the form, you can access it by calling Request.Form without any parameters. – Frédéric May 31 '19 at 16:10
  • But unfortunately, it does not work for a JSon content. This yield an empty string instead of the JSON raw text. – Frédéric May 31 '19 at 21:10
1

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.

AnonJr
  • 2,759
  • 1
  • 26
  • 39
  • Thank you for your help. My client (ios)is able to basically set a request body when POSTing to a URL. [request setHTTPBody:requestData]; The client wasn't using a form or anything. The payload being sent from the client is a String (although it's wrapped in an NSData object by iOS) So I thought that POSTed data did not have to be attached to Request.Form. Hence my confusion. – user798719 Mar 19 '12 at 19:51
  • Does this mean that I need to add key-value pair when POSTing from the ios client? I don't see how I'd access the message body once it has reached my asp form – user798719 Mar 19 '12 at 20:01