如何使用VBScript通过HTTP API发布JSON数据?

Raj*_*n R 8 vbscript json xmlhttprequest

尝试将JSON POST请求发送到HTTP API(PushBullet).但是在代码中面临错误.任何帮助赞赏.参考文件发送推送通知

Dim objXmlHttpMain , URL

strJSONToSend = {"type": "note", "title": "Alert", "body": "Lorem Ipsum Lorem Ipsum Lorem Ipsum."}

URL="https://api.pushbullet.com/v2/pushes" 
Set objXmlHttpMain = CreateObject("Msxml2.ServerXMLHTTP") 
on error resume next 
objXmlHttpMain.open "POST",URL, False 
objXmlHttpMain.setRequestHeader "Authorization", "Bearer <api secret id>"
objXmlHttpMain.setRequestHeader "Content-Type", "application/json"


objXmlHttpMain.send strJSONToSend

set objJSONDoc = nothing 
set objResult = nothing
Run Code Online (Sandbox Code Playgroud)

Ans*_*ers 15

在这里走出困境,因为您认为没有必要包含实际的错误消息.您很可能在第3行中收到"无效字符"错误.这是因为您需要将JSON字符串定义为实际字符串.

改变这个:

strJSONToSend = {"type": "note", "title": "Alert", "body": "Lorem Ipsum Lorem Ipsum Lorem Ipsum."}
Run Code Online (Sandbox Code Playgroud)

进入这个:

strJSONToSend = "{""type"": ""note"", ""title"": ""Alert"", ""body"": ""Lorem Ipsum Lorem Ipsum Lorem Ipsum.""}"
Run Code Online (Sandbox Code Playgroud)

编辑:作为旁注,如果您On Error Resume Next在代码中使用,请始终将适当的错误处理放在适当的位置,并尽可能保持本地化:

On Error Resume Next   'enable error handling
objXmlHttpMain.open "POST",URL, False
If Err Then            'handle errors
  WScript.Echo Err.Description & " [0x" & Hex(Err.Number) & "]"
  WScript.Quit 1
End If
On Error Goto 0        'disable error handling again
Run Code Online (Sandbox Code Playgroud)