I need to POST
data to a url in the middle of a script.
process.asp
: I need to PO
Most form action pages accept data as a POST.
Function postFormData(url, data)
Dim xhr : Set xhr = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
xhr.open "POST", url, false
xhr.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xhr.send Data
If xhr.Status = 200 Then
postFormData = xhr.ResponseText
Else
Err.Raise 1001, "postFormData", "Post to " & url & " failed with " & xhr.Status
End If
End Function
When creating the data url encoding is needed on the data values. Since ASPs Server.URLEncode method only does path encoding and not component encoding you need to replace out / characters with %2F
Function URLEncodeComponent(value)
URLEncodeComponent = Server.URLEncode(value)
URLEncodeComponent = Replace(URLEncodeComponent, "/", "%2F")
End Function
OK all the answers were very complicated and I know you already selected a solution - but I feel like the simple Server.Transfer() command could have done exactly what you need.
At the end of your script instead of Response.Redirect(url) to the new page - just do a Server.Transfer(url) and it will pass your entire Request collection across to the next page.
Read about it here (support.microsoft.com).
There are some catches (i.e. it keeps the same URL on the browser so it can play tricks with the back button and such) but otherwise it's pretty simple.