1

To test, I am sending the data using curl like so:

curl -XPOST http://test.local/kms/test.asp --data "{\"a\":1212321}" -H 'Content-Type: application/json'

In test.asp I have something like this:

var byteArray = Request.BinaryRead(Request.TotalBytes);

From there I try to convert each byte to a character and append it to a string, however accessing the information seems to be the issue. This is one attempt I have tried:

 var str = "";
 for (var x = 0; x < Request.TotalBytes; x++) {
     str += String.fromCharCode(byteArray[x]);
 }

When I check in visual studio, the data looks like this:

display of Array of Bytes

Is there a better way to get data from a request?

1 Answer 1

3

Accessing an Array of Byte and converting it into a string

For server-side JScript, and using your curl POST example above, try this:

var postData = null;
with (Server.CreateObject("ADODB.Stream")) {
    Type = 1; // adTypeBinary
    Open();
    Write(Request.BinaryRead(Request.TotalBytes));
    Position = 0;
    Type = 2; // adTypeText
    Charset = "iso-8859-1";
    postData = ReadText();
    Close();
}
if (postData) {
    var json = null;
    eval("json = " + postData);
    Response.Write(json.a);
}

This solution uses the ADODB.Stream object to write-in the binary data from the Request, reset the position back to the beginning of the stream, then convert the binary data to a properly encoded string by reading it into a variable (postData). If postData exists, evaluate the JSON string as-is, then use it.


Is there a better way to get data from a request?

Perhaps. You could POST the data as 'application/x-www-form-urlencoded', then simply access the variables using Request.Form("a").

For example, try changing your curl POST command to:

curl -XPOST http://test.local/kms/test.asp --data "a=1212321" -H 'Content-Type:application/x-www-form-urlencoded'

Then update your server-side ASP JScript code to resemble:

var str = Request.Form("a");
Response.Write(str);

Hope this is helpful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.