Discussion:
vbscript post to REST API HELP
(too old to reply)
s***@gmail.com
2017-02-22 20:17:05 UTC
Permalink
Hi,

New to vbscript and trying to troubleshoot an "Unspecified error"

I have been trying to glue this together from search results. It seems to try and run but getting the generic 80004005 error code which isn't helping. Small script hope someone can spot the problem.

Option Explicit
Dim objXmlHttpMain, user , password , URL, strJSONToSend

user = "testuser"
password = "testpass"
URL="https://testsitname.com/api/alert"

strJSONToSend = "{aligned_resource"": ""/device/17653"", ""message"": ""TESTING API""}"


Set objXmlHttpMain = CreateObject("Msxml2.ServerXMLHTTP")
On Error Resume Next
objXmlHttpMain.setRequestHeader "Content-Type", "application/json"
objXmlHttpMain.send strJSONToSend
objXmlHttpMain.open "POST",URL, False, user, password

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
Evertjan.
2017-02-22 22:06:17 UTC
Permalink
Post by s***@gmail.com
Hi,
New to vbscript and trying to troubleshoot an "Unspecified error"
I have been trying to glue this together from search results. It seems
to try and run but getting the generic 80004005 error code which isn't
helping. Small script hope someone can spot the problem.
Option Explicit
Dim objXmlHttpMain, user , password , URL, strJSONToSend
user = "testuser"
password = "testpass"
URL="https://testsitname.com/api/alert"
strJSONToSend = "{aligned_resource"": ""/device/17653"", ""message"": ""TESTING API""}"
Set objXmlHttpMain = CreateObject("Msxml2.ServerXMLHTTP")
On Error Resume Next
objXmlHttpMain.setRequestHeader "Content-Type", "application/json"
objXmlHttpMain.send strJSONToSend
objXmlHttpMain.open "POST",URL, False, user, password
You should first .open something and only then .send it.
Post by s***@gmail.com
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
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Scot Needy
2017-02-23 01:14:21 UTC
Permalink
Thanks Evertjan I caught that after I posted. So many changes just to try and get this working.

Still getting 80004005 "unspecified error" I suspect it has something to do with the Msxml2.ServerXMLHTTP methods i'm using but I'm having trouble finding good examples.


Option Explicit
Dim objXmlHttpMain, user , password , URL, strJSONToSend, TaskName

user = "testuser"
password = "testpass"
URL="https://testsitename.com/api/alert"
TaskName = "TestJob"


strJSONToSend = "{aligned_resource"": ""/device/17653"", ""message"": ""Taskname Failed""}"

Set objXmlHttpMain = CreateObject("Msxml2.ServerXMLHTTP")
On Error Resume Next
objXmlHttpMain.open "POST",URL, False
objXmlHttpMain.setRequestHeader "Content-Type", "application/json"
objXmlHttpMain.send("username=user&password=password")
objXmlHttpMain.send(strJSONToSend)

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
Scot Needy
2017-02-23 05:22:34 UTC
Permalink
So I changed to http and ran some tcpdumps on the webserver side.

The POST is getting sent but for some reason the vbscript is getting a 404.
I took the username:password and Base64 encoded them for the Auth Header.
From a packetdump the Authorization looks identical!! I don't see why this would fail.

strJSONToSend = "{""aligned_resource"": ""/device/12518"", ""message"": ""Task Failed""}"
url = "http://testsitename.com/api/alert"
Set xmlhttp = CreateObject("MSXML2.serverXMLHTTP")
on error resume next
xmlhttp.open "POST", url, false, username, password
xmlhttp.setRequestHeader "Authorization", "Basic b3Nhc********DFvbGllbTc="
xmlhttp.setRequestHeader "Content-Type", "application/json"
xmlhttp.send(strJSONToSend)



I compared this against a packet capture of a successful curl command
curl -i -k -H "Content-Type:application/json" -u "${username}:${password}" -X POST $address -d '{"aligned_resource":"/device/12518","message":"TESTING"}'
JJ
2017-02-24 01:20:12 UTC
Permalink
Post by Scot Needy
So I changed to http and ran some tcpdumps on the webserver side.
The POST is getting sent but for some reason the vbscript is getting a 404.
You should provide an actual working URL, because the URL that you've
provided points to a non existant resource in the server.

http://testsitename.com/api/alert

Will always result to HTTP code 404. Whether a credential is given or not.
Scot Needy
2017-02-24 04:03:49 UTC
Permalink
Post by JJ
Post by Scot Needy
So I changed to http and ran some tcpdumps on the webserver side.
The POST is getting sent but for some reason the vbscript is getting a 404.
You should provide an actual working URL, because the URL that you've
provided points to a non existant resource in the server.
http://testsitename.com/api/alert
Will always result to HTTP code 404. Whether a credential is given or not.
Yea JJ but I needed to modify the real values to be somewhat anonymous besides the actual URL is on private ip space and DNS. Would not have helped in a public forum.

Glad to say I have my simple post working after gutting it of any fluff. Now I need to add it back in but really wasn't sure it was working right in the first place which is why I posted an open question about logical error checking.

To me, the first two places something like this would fail on is authorization or error from open or send methods. Also I still don't understand if/how this works. Seems like there should be more.
Set objXmlHttpMain = CreateObject("Msxml2.XMLHTTP")
on error resume next

Seems like vbs using Msxml2 is pretty rare from google searches so I'm just trying to piece it together from search results.

Going to start looking at these examples to start. I'm sure I'll get stuck along the lines.
https://coderwall.com/p/ljvj-g/unlikely-coding-a-vbscript-posting-to-a-rest-api
http://stackoverflow.com/questions/26956256/how-to-post-json-data-via-http-api-using-vbscript
http://stackoverflow.com/questions/24863986/vbscript-msxml12-xmlhttp-error-handling



This site had a pretty good breakdown of XmlHttp
http://www.c-amie.co.uk/technical/javascript-serverxmlhttp-emulation/
Evertjan.
2017-02-23 09:50:14 UTC
Permalink
Post by Scot Needy
Thanks Evertjan I caught that after I posted. So many changes just to
try and get this working.
Still getting 80004005 "unspecified error" I suspect it has something to
do with the Msxml2.ServerXMLHTTP methods i'm using but I'm having
trouble finding good examples.
[..]
Post by Scot Needy
strJSONToSend = "{aligned_resource"": ""/device/17653"", ""message"": ""Taskname Failed""}"
strJSONToSend = "{""aligned_resource"":.....

But probably, that is not what thows the error.

Please start by debugging, programming in breakpoints, etc.

We should not do this testing for you.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Scot Needy
2017-02-23 12:54:59 UTC
Permalink
Post by Evertjan.
Post by Scot Needy
Thanks Evertjan I caught that after I posted. So many changes just to
try and get this working.
Still getting 80004005 "unspecified error" I suspect it has something to
do with the Msxml2.ServerXMLHTTP methods i'm using but I'm having
trouble finding good examples.
[..]
Post by Scot Needy
""Taskname Failed""}"
strJSONToSend = "{""aligned_resource"":.....
But probably, that is not what thows the error.
Please start by debugging, programming in breakpoints, etc.
We should not do this testing for you.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Evertjan,

Your correction is not what I posted and does not address the error as you stated.
Your assuming I'm just throwing this out there without debugging ? I'm reaching out because I have spent the last 20+ hours trying to get a simple curl one line into vbs.

My problem was the base64 encoded string I created it had encoded the ":" and gave me 2 incorrect characters in the string. I changed it to what I captured and the code now works but needs error checking.
I tried getting and example I saw to dynamically encode but failed completely.
Removed credentials from the open statement and using the encoded string only.
xmlhttp.open "POST", url, false
xmlhttp.setRequestHeader "Authorization", "Basic ******encoded string****"
xmlhttp.setRequestHeader "Content-Type", "application/json"
xmlhttp.send(strJSONToSend)


Anyone have good working example of error condition checks ? I've tried all the ones I found on Google.
Evertjan.
2017-02-23 13:06:20 UTC
Permalink
Post by Scot Needy
Post by Evertjan.
Post by Scot Needy
Thanks Evertjan I caught that after I posted. So many changes just to
try and get this working.
Still getting 80004005 "unspecified error" I suspect it has something
to do with the Msxml2.ServerXMLHTTP methods i'm using but I'm having
trouble finding good examples.
[..]
Post by Scot Needy
""Taskname Failed""}"
strJSONToSend = "{""aligned_resource"":.....
But probably, that is not what thows the error.
Please start by debugging, programming in breakpoints, etc.
We should not do this testing for you.
[please do not quote signatures on usenet, signature removed]
Post by Scot Needy
Your correction is not what I posted and does not address the error as you stated.
Your assuming I'm just throwing this out there without
debugging ?
Well, what else could I assume
if you do not give the fact and the the results of your debugging.
Post by Scot Needy
I'm reaching out because I have spent the last 20+ hours
trying to get a simple curl one line into vbs.
My problem was the base64 encoded string I created it had encoded the
":" and gave me 2 incorrect characters in the string.
That is rather a different story, isn't it?

Did you give the code for that creation?
Post by Scot Needy
I changed it to
what I captured and the code now works but needs error checking. I
tried getting and example I saw to dynamically encode but failed
completely.
Why would it need error-checking if it works?

Please specify what error[s] you expect to catch.
Post by Scot Needy
Removed credentials from the open statement and using the
encoded string only.
xmlhttp.open "POST", url, false
xmlhttp.setRequestHeader "Authorization", "Basic ******encoded
string****"
xmlhttp.setRequestHeader "Content-Type", "application/json"
xmlhttp.send(strJSONToSend)
Anyone have good working example of error condition checks ? I've tried
all the ones I found on Google.
See above.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Scot Needy
2017-02-23 17:10:11 UTC
Permalink
Post by Evertjan.
Post by Scot Needy
Post by Evertjan.
Post by Scot Needy
Thanks Evertjan I caught that after I posted. So many changes just to
try and get this working.
Still getting 80004005 "unspecified error" I suspect it has something
to do with the Msxml2.ServerXMLHTTP methods i'm using but I'm having
trouble finding good examples.
[..]
Post by Scot Needy
""Taskname Failed""}"
strJSONToSend = "{""aligned_resource"":.....
But probably, that is not what thows the error.
Please start by debugging, programming in breakpoints, etc.
We should not do this testing for you.
[please do not quote signatures on usenet, signature removed]
Post by Scot Needy
Your correction is not what I posted and does not address the error as you stated.
Your assuming I'm just throwing this out there without
debugging ?
Well, what else could I assume
if you do not give the fact and the the results of your debugging.
Post by Scot Needy
I'm reaching out because I have spent the last 20+ hours
trying to get a simple curl one line into vbs.
My problem was the base64 encoded string I created it had encoded the
":" and gave me 2 incorrect characters in the string.
That is rather a different story, isn't it?
Did you give the code for that creation?
Post by Scot Needy
I changed it to
what I captured and the code now works but needs error checking. I
tried getting and example I saw to dynamically encode but failed
completely.
Why would it need error-checking if it works?
Please specify what error[s] you expect to catch.
Post by Scot Needy
Removed credentials from the open statement and using the
encoded string only.
xmlhttp.open "POST", url, false
xmlhttp.setRequestHeader "Authorization", "Basic ******encoded string****"
xmlhttp.setRequestHeader "Content-Type", "application/json"
xmlhttp.send(strJSONToSend)
Anyone have good working example of error condition checks ? I've tried
all the ones I found on Google.
See above.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Evertjan I really don't need your condescending attitude. Pack your ego away if your here to help.

"Why would it need error-checking if it works? " REALLY ?
Evertjan.
2017-02-23 19:02:22 UTC
Permalink
This post might be inappropriate. Click to display it.
Mayayana
2017-02-23 22:19:21 UTC
Permalink
"Scot Needy" <***@gmail.com> wrote

| Evertjan I really don't need your condescending attitude.

There are a few OCD types here. Unfortunately
for you, one of them seems to be the only person
who uses xmlhttp. :)

If you need to fix the base64 conversion I have
VBScript for that, but I haven't done anything like
what you're trying, so I can't be helpful on that score.
Scot Needy
2017-02-24 03:32:06 UTC
Permalink
Post by Mayayana
| Evertjan I really don't need your condescending attitude.
There are a few OCD types here. Unfortunately
for you, one of them seems to be the only person
who uses xmlhttp. :)
If you need to fix the base64 conversion I have
VBScript for that, but I haven't done anything like
what you're trying, so I can't be helpful on that score.
Thanks Mayayana, I tried using a inline encoder like below but viewing the TCP dump it was not encoding it correctly and I actually like have a encoded password in my script now.
It might be simple to crack but at least it's not out there in plain text. We have a password vault that stored the real id/password if it's needed.

Probably could have got this working if I played with it more.
XMLHTTP.setRequestHeader "Authorization", "Basic " & Base64Encode(apikey & ":" & pass)

I have the simple post working now but I pulled out all the guts to make it a simple as possible to debug any "unknown errors". Now I just need to put the obvious error checking back. A alert script that fails with no warning is not worth a kb.
Mayayana
2017-02-24 04:07:15 UTC
Permalink
"Scot Needy" <***@gmail.com> wrote

| Thanks Mayayana, I tried using a inline encoder like below but viewing the
TCP dump it was not encoding it correctly and I actually like have a encoded
password in my script now.
|

If you end up needing it, see here:

http://www.jsware.net/jsware/scrfiles.php5#bints

The file Basic Base64- Encode- Decode.vbs in the download
contains 2 compact, all-VBS functions to encode/decode.
Evertjan.
2017-02-24 08:17:31 UTC
Permalink
Post by Mayayana
| Thanks Mayayana, I tried using a inline encoder like below but viewing
| the
TCP dump it was not encoding it correctly and I actually like have a
encoded password in my script now.
|
http://www.jsware.net/jsware/scrfiles.php5#bints
The file Basic Base64- Encode- Decode.vbs in the download
contains 2 compact, all-VBS functions to encode/decode.
The below looks promising [but not tested by me],
given that the Q is how to use json in and output with cscript and vbs,
with base64 encoding:

<https://coderwall.com/p/ljvj-g/unlikely-coding-a-vbscript-posting-to-a-
rest-api>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mayayana
2017-02-24 15:22:14 UTC
Permalink
"Evertjan." <***@inter.nl.net> wrote

| The below looks promising [but not tested by me],
| given that the Q is how to use json in and output with cscript and vbs,
| with base64 encoding:
|
| <https://coderwall.com/p/ljvj-g/unlikely-coding-a-vbscript-posting-to-a-
| rest-api>
|

You know more about this than I do. I've never
used xmlhttp, nor had any reason to do anything
similar. I was only addressing what I understood to
be a faulty Base64 routine. What you linked is a
routine to convert a URL to remove trigger characters.
d***@gmail.com
2020-01-13 17:15:55 UTC
Permalink
Hi all, related to this origional post,,,,, for base64encode, there is a known issue. When the encoding the 76th character encoding inserts a vbLf Chr(10). Use vbscript statement to remove that character string.

strEnc = Replace(strEnc, vbLf, "")

https://stackoverflow.com/questions/41837881/base64-encode-with-stream-stringtobinary-inserts-a-newline-breaking-the-string
Loading...