Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vbscript post to REST API HELP

2,643 views
Skip to first unread message

sco...@gmail.com

unread,
Feb 22, 2017, 3:17:06 PM2/22/17
to
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.

unread,
Feb 22, 2017, 5:06:18 PM2/22/17
to
sco...@gmail.com wrote on 22 Feb 2017 in
microsoft.public.scripting.vbscript:

> 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.


> 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

unread,
Feb 22, 2017, 8:14:26 PM2/22/17
to
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)

Scot Needy

unread,
Feb 23, 2017, 12:22:36 AM2/23/17
to
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"}'

Evertjan.

unread,
Feb 23, 2017, 4:50:17 AM2/23/17
to
Scot Needy <sco...@gmail.com> wrote on 23 Feb 2017 in
microsoft.public.scripting.vbscript:

> 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.
[..]
> 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.

Scot Needy

unread,
Feb 23, 2017, 7:55:03 AM2/23/17
to
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.

unread,
Feb 23, 2017, 8:06:53 AM2/23/17
to
Scot Needy <sco...@gmail.com> wrote on 23 Feb 2017 in
microsoft.public.scripting.vbscript:

> On Thursday, February 23, 2017 at 4:50:17 AM UTC-5, Evertjan. wrote:
>> Scot Needy <sco...@gmail.com> wrote on 23 Feb 2017 in
>> microsoft.public.scripting.vbscript:
>>
>> > 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.
>> [..]
>> > 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.

[please do not quote signatures on usenet, signature removed]

> 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.

> 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?

> 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.

> 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.

Scot Needy

unread,
Feb 23, 2017, 12:10:12 PM2/23/17
to
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.

unread,
Feb 23, 2017, 2:02:24 PM2/23/17
to
Scot Needy <sco...@gmail.com> wrote on 23 Feb 2017 in
microsoft.public.scripting.vbscript:

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

Dear Scot,

This NG is not to care for your needs, Usenet-NGs are discussion-groups.

> Pack your ego away if your here to help.

And they are not helpdesks, where the OP can define the limits of the
resopnses.

> "Why would it need error-checking if it works? " REALLY ?

Please do not shout on Usenet, this is not a presidential rally.

Yes really.

If you want to do error-checking, it is good programming practice that you
define what errors you want to check and what not.

For instance, you do not need to catch programming errors, like
setRequestHeader-errors or trying to .send before .open, id the code "works".

Perhaps you want to check if the destination is online?

I would not call that an error in casu strictior, but an intermittent nasty
fact of life, but you could mean that.

So please tell what you really [!!] want instead of feeling wronged.

Mayayana

unread,
Feb 23, 2017, 5:19:33 PM2/23/17
to
"Scot Needy" <sco...@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.


JJ

unread,
Feb 23, 2017, 8:20:20 PM2/23/17
to
On Wed, 22 Feb 2017 21:22:34 -0800 (PST), Scot Needy wrote:
> 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

unread,
Feb 23, 2017, 10:32:09 PM2/23/17
to
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.



Scot Needy

unread,
Feb 23, 2017, 11:03:52 PM2/23/17
to
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/

Mayayana

unread,
Feb 23, 2017, 11:07:28 PM2/23/17
to
"Scot Needy" <sco...@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.

unread,
Feb 24, 2017, 3:18:11 AM2/24/17
to
"Mayayana" <maya...@invalid.nospam> wrote on 24 Feb 2017 in
microsoft.public.scripting.vbscript:
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>

Mayayana

unread,
Feb 24, 2017, 10:22:27 AM2/24/17
to
"Evertjan." <exxjxw.h...@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.


Message has been deleted
0 new messages