如何使用 VBScript 通过 HTTP API POST JSON 数据? [英] How to POST JSON Data via HTTP API using VBScript?

查看:37
本文介绍了如何使用 VBScript 通过 HTTP API POST JSON 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试向 HTTP API(PushBullet) 发送 JSON POST 请求.但面临代码中的错误.任何帮助表示赞赏.参考文档发送推送通知

Trying to Send JSON POST Request to an HTTP API(PushBullet). But facing error in the code. Any help appreciated. Reference to the document to send push notification

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

推荐答案

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

Going out on a limb here, since you didn't deem it necessary to include the actual error message. You're most likely getting an "invalid character" error in line 3. That's because you need to define your JSON string as an actual string.

改变这个:

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

进入这个:

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

<小时>

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


As a side-note, if you're using On Error Resume Next in your code always put proper error handling in place, and also keep it as localized as possible:

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

这篇关于如何使用 VBScript 通过 HTTP API POST JSON 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆