How to send POST JSON data via HTTP API using VBScript?

Trying to send a JSON POST request to the HTTP API (PushBullet). But they encounter an error in the code. Any help appreciated. Link to a document for sending push notifications

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 
+7
source share
1 answer

Exit to the limb here, since you did not consider it necessary to include the actual error message. Most likely, you will get an “invalid character” error on line 3. This is because you need to define your JSON string as the actual string.

Change this:

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

in it:

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

Edit: As a note, if you use On Error Resume Next in your code, always set the correct 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 
+12
source

Source: https://habr.com/ru/post/978252/


All Articles