12

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

1 Answer 1

19

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.

Change this:

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

into this:

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

Edit: 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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot that worked! Iam sorry iam very new to vbscript!
Can this be wrapped in a sub or function and called when / if needed?
@NCollinsTE Sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.