如何使用VBS发布漂亮的JSON

时间:2018-04-10 17:07:04

标签: json http post vbscript

我正在尝试构建一个脚本,该脚本将以JSON格式生成帖子,并将其发送给我已设置进行测试的侦听器。为了我的目的,我希望得到我的特定信息(" Hello world"与我生成的UUID一起发送。我不会轻易查询这两个对象,但我得到的问题是当我嗅到流量时,它看起来像这样:

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

我试图找出摆脱斜线的最佳方法.....我知道它们被用来排除双引号,但我需要那些适当的json格式...我确信有一种更简单的方法可以做我想尝试的事情,但我根本不熟悉VB。如果有人可以建议如何轻松摆脱这些,或者如果有更好的方式发送json,我将不胜感激。

继承我的守则:

'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" )

1 个答案:

答案 0 :(得分:2)

感谢@Lankymart识别上面错误的应用程序类型,这是解决方案。它必须切换到appication / JSON,它缺少一些引号。

'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" )