Vincent Morris
Vincent Morris

Reputation: 670

How to Post Pretty JSON with VBS

I'm trying to build a script that will generate a post in JSON format, and send it to a listener I have setup for testing. For my purpose I want to have my specific message("Hello world" sent along with a UUID I generate. I wont both of these objects to be easily queried, but the problem I get is when I sniff the traffic, it looks like this:

 "form": {
    "{\"UUID\": \"{D3F8FC28-C5FE-4B73-ADA4-FD459267B067}\" , \"Post\": hello world}": ""
  }, 

I'm trying to figure out the best way to get rid of the slashes..... I know they are put in to exclude the double quotes, but I need those to be proper json format.... I'm sure there is an easier way to do what I'm trying, but I'm not familar with VBs at all. If anyone could advise on how to get rid of the '\'s easily, or if there is a better way to send json, I would appreciate it.

Heres my Code:

'URL of listener
sUrl = "http://httpbin.org/post"
'Generate uuid
Set TypeLib = CreateObject("Scriptlet.TypeLib")
uuid = TypeLib.Guid
uuid = Left(uuid, Len(uuid)-2)
post = "Hello world"
sRequest = "{""UUID"": """ & UUID & """ , ""Post"": "& post &"}"

'function needed to send post
HTTPPost sUrl, sRequest
Function HTTPPost(sUrl, sRequest)
  set oHTTP = CreateObject("Microsoft.XMLHTTP")
  oHTTP.open "POST", sUrl,false
  oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  oHTTP.setRequestHeader "Content-Length", Len(sRequest)
  oHTTP.send sRequest
  HTTPPost = oHTTP.responseText
 End Function

Set wshShell = CreateObject( "WScript.Shell" )

Upvotes: 2

Views: 1069

Answers (1)

Vincent Morris
Vincent Morris

Reputation: 670

Credit to @Lankymart for identifying the wrong application type above, here is the solution. It had to be switched to appication/JSON and it was missing some quotes.

'URL of listener
sUrl = "http://httpbin.org/post"
'Generate uuid
Set TypeLib = CreateObject("Scriptlet.TypeLib")
uuid = TypeLib.Guid
uuid = Left(uuid, Len(uuid)-2)
post = "Hello world"
sRequest = "{""UUID"": """ & UUID & """ , ""Post"": """& post &"""}"

'function needed to send post
HTTPPost sUrl, sRequest
Function HTTPPost(sUrl, sRequest)
  set oHTTP = CreateObject("Microsoft.XMLHTTP")
  oHTTP.open "POST", sUrl,false
  oHTTP.setRequestHeader "Content-Type", "application/json"
  oHTTP.setRequestHeader "Content-Length", Len(sRequest)
  oHTTP.send sRequest
  HTTPPost = oHTTP.responseText
 End Function

Set wshShell = CreateObject( "WScript.Shell" )

Upvotes: 2

Related Questions