Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a classic ASP page using xmlhttp make a JSON request?

I am completely lost. I am trying to post to an API on a remote server from a classic asp page that uses vbscript. My code:

set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
xmlhttp.open "POST", vURL, false 
xmlhttp.setRequestHeader "Content-type","application/json"
xmlhttp.setRequestHeader "Accept","application/json"
xmlhttp.send "[email protected]&firstname=joe&lastname=smith"
vAnswer = xmlhttp.responseText  

I receive a response that the request is not in the expected format. Tech support informs me that the API expects JSON in the post body. Can I do this from server-side asp?

like image 973
user1048348 Avatar asked Nov 15 '11 23:11

user1048348


4 Answers

'Create a function
Function ASPPostJSON(url)

'declare a variable
Dim objXmlHttp

Set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")

'If the API needs userName and Password authentication then pass the values here
objXmlHttp.Open "POST", url, False, "User123", "pass123"
objXmlHttp.SetRequestHeader "Content-Type", "application/json"
objXmlHttp.SetRequestHeader "User-Agent", "ASP/3.0"

'send the json string to the API server
objXmlHttp.Send "{""TestId"": 012345,""Test1Id"": 123456,""Test123"": 37,""Type123"": ""Test_String"",""contact"": {""name"": ""FirstName LastName"",""Organization"": ""XYZ"",""phone"":""123456"",""emailAddress"": ""[email protected]""}}"

'If objXmlHttp.Status = 200 Then
    ASPPostJSON = CStr(objXmlHttp.ResponseText)
'end if

'return the response from the API server
Response.write(ASPPostJSON)
Set objXmlHttp = Nothing

End Function

'call the function and pass the API URL
call ASPPostJSON("https://TheAPIUrl.com/")
like image 188
Anonymous Avatar answered Oct 31 '22 15:10

Anonymous


Because I ran across this when effectively trying to solve the same issue -- in my case, POSTing from ASP Classic to the MailChimp 2.0 API -- I wanted to echo the helpfulness of the http://code.google.com/p/aspjson/ link, but also note something that was at least relevant in the case of MailChimp. I thought I could simply format a JSON-looking string and send that, but it didn't work. I had to create a JSON object using the methods in the aspjson library, and then use the jsString method in the send statement. So the code snippet (after appropriate declarations, includes, etc.) would look something like this:

Set objJSON = jsObject()
objJSON("apikey") = "MY API KEY"
Set objJSON("email") = jsObject()
objJSON("email")("email") = strEmail
Set objXMLhttp = Server.Createobject("MSXML2.ServerXMLHTTP")
objXMLhttp.open "POST","https://mailchimpurl/2.0/helper/lists-for-email", false
objXMLhttp.setRequestHeader "Content-type","application/json"
objXMLhttp.setRequestHeader "Accept","application/json"
objXMLhttp.send objJSON.jsString
strResponse = objXMLhttp.responseText
Set objXMLhttp = Nothing

Hope that helps someone else along the way.

like image 30
Bret Avatar answered Oct 31 '22 15:10

Bret


Check this out, it should help you do what you want:

http://code.google.com/p/aspjson/

Good luck!

like image 1
Bugget Avatar answered Oct 31 '22 14:10

Bugget


The request you are sending is ..... not JSON. Try use this as a validator: JSONLint. Chuck your JSON string into there and it will tell you if it's valid or not. In the case above: [email protected]&firstname=joe&lastname=smith. It is most definitely not.

You can write JSON by hand, for example I would rewrite your query as follows:

{"Email":"[email protected]", "firstname":"joe", "lastname":"smith"}

I hope that helps. Yes, there are libraries that can help you do this (ASPJSON is one of them) but to be honest I prefer writing them out myself (ASP is so unwieldy) or writing my own functions because I know that I can trust them. Here is an example piece of code I wrote in ASP that can make a JSON string from a Dictionary object. It can also have arrays inside the dictionary elements. Unfortunately it's not recursive so it can't do arrays of arrays or dictionaries of dictionaries ... but it works quiet well for simple inputs. Named json_encode after the PHP function.

Function json_encode(ByVal dic)
    ret = "{"
    If TypeName(dic) = "Dictionary" Then
        For each k in dic
            Select Case VarType(dic.Item(k))
                Case vbString
                    ret = ret & """" & k & """:""" & dic.Item(k) & ""","
                Case Else
                    If VarType(dic.Item(k)) > vbArray Then
                        ret = ret & """" & k & """:["
                        For x = 0 to Ubound(dic.Item(k), 1)
                            ret = ret & """" & dic.Item(k)(x) & ""","
                        Next
                        ret = Left(ret, Len(ret) - 1)   'Trim trailing comma
                        ret = ret & "],"
                    Else
                        ret = ret & """" & k & """:""" & dic.Item(k) & ""","
                    End If
            End Select
        Next
        ret = Left(ret, Len(ret) - 1)   'Trim trailing comma
    End If
    ret = ret & "}"
    json_encode = ret
End Function
like image 1
MikeMurko Avatar answered Oct 31 '22 14:10

MikeMurko