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:
我完全迷失了。我试图从使用vbscript的经典asp页面发布到远程服务器上的API。我的代码:
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "POST", vURL, false
xmlhttp.setRequestHeader "Content-type","application/json"
xmlhttp.setRequestHeader "Accept","application/json"
xmlhttp.send "email=asdf@hotmail.com&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?
我收到的回复是请求不是预期的格式。技术支持通知我,API期望在帖子正文中使用JSON。我可以从服务器端asp执行此操作吗?
4 个解决方案
#1
3
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:
因为我在有效地尝试解决同样的问题时遇到了这个问题 - 在我的案例中,从ASP Classic发布到MailChimp 2.0 API - 我想回应http://code.google.com/p/的帮助aspjson / link,但也注意到在MailChimp的情况下至少相关的东西。我以为我可以简单地格式化一个看起来像JSON的字符串并发送它,但它不起作用。我必须使用aspjson库中的方法创建一个JSON对象,然后在send语句中使用jsString方法。所以代码片段(在适当的声明,包括等之后)看起来像这样:
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.
希望在此过程中帮助其他人。
#2
2
'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"": ""test@mail.com""}}"
'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/")
#3
1
Check this out, it should help you do what you want:
看看这个,它可以帮助你做你想要的:
http://code.google.com/p/aspjson/
Good luck!
#4
1
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=asdf@hotmail.com&firstname=joe&lastname=smith
. It is most definitely not.
您发送的请求是.....而不是JSON。尝试将其用作验证器:JSONLint。将你的JSON字符串放入那里,它会告诉你它是否有效。在上面的例子中:email=asdf@hotmail.com&firstname=joe&lastname=smith。绝对不是。
You can write JSON by hand, for example I would rewrite your query as follows:
您可以手动编写JSON,例如我会按如下方式重写您的查询:
{"Email":"asdf@hotmail.com", "firstname":"joe", "lastname":"smith"}
{“Email”:“asdf @ hotmail.com”,“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.
我希望有所帮助。是的,有些库可以帮助你做到这一点(ASPJSON就是其中之一),但说实话,我更喜欢自己编写(ASP太笨重)或编写我自己的函数,因为我知道我可以信任它们。这是我在ASP中编写的一段代码示例,它可以从Dictionary对象生成一个JSON字符串。它也可以在字典元素中包含数组。不幸的是它不是递归的,所以它不能做数组的数组或词典的字典...但它对于简单的输入很安静。在PHP函数之后命名为json_encode。
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
#1
3
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:
因为我在有效地尝试解决同样的问题时遇到了这个问题 - 在我的案例中,从ASP Classic发布到MailChimp 2.0 API - 我想回应http://code.google.com/p/的帮助aspjson / link,但也注意到在MailChimp的情况下至少相关的东西。我以为我可以简单地格式化一个看起来像JSON的字符串并发送它,但它不起作用。我必须使用aspjson库中的方法创建一个JSON对象,然后在send语句中使用jsString方法。所以代码片段(在适当的声明,包括等之后)看起来像这样:
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.
希望在此过程中帮助其他人。
#2
2
'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"": ""test@mail.com""}}"
'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/")
#3
1
Check this out, it should help you do what you want:
看看这个,它可以帮助你做你想要的:
http://code.google.com/p/aspjson/
Good luck!
#4
1
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=asdf@hotmail.com&firstname=joe&lastname=smith
. It is most definitely not.
您发送的请求是.....而不是JSON。尝试将其用作验证器:JSONLint。将你的JSON字符串放入那里,它会告诉你它是否有效。在上面的例子中:email=asdf@hotmail.com&firstname=joe&lastname=smith。绝对不是。
You can write JSON by hand, for example I would rewrite your query as follows:
您可以手动编写JSON,例如我会按如下方式重写您的查询:
{"Email":"asdf@hotmail.com", "firstname":"joe", "lastname":"smith"}
{“Email”:“asdf @ hotmail.com”,“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.
我希望有所帮助。是的,有些库可以帮助你做到这一点(ASPJSON就是其中之一),但说实话,我更喜欢自己编写(ASP太笨重)或编写我自己的函数,因为我知道我可以信任它们。这是我在ASP中编写的一段代码示例,它可以从Dictionary对象生成一个JSON字符串。它也可以在字典元素中包含数组。不幸的是它不是递归的,所以它不能做数组的数组或词典的字典...但它对于简单的输入很安静。在PHP函数之后命名为json_encode。
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