【问题标题】:How to Post Pretty JSON with VBS如何使用 VBS 发布漂亮的 JSON
【发布时间】:2018-04-10 17:07:04
【问题描述】:

我正在尝试构建一个脚本,该脚本将生成 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" )

【问题讨论】:

  • 您正在发送 JSON,因此您应该将 Content-Type 设置为 application/json 而不是 application/x-www-form-urlencoded,这意味着发送 URL 编码的字符串键值对而不是 JSON。
  • 哦,你是对的!!!我忘了这一切谢谢!

标签: json http post vbscript


【解决方案1】:

感谢@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" )

【讨论】:

  • 在那个例子中你仍然使用了错误的Content-Type
  • oops copynpasta 失败
猜你喜欢
  • 2016-12-31
  • 2013-10-03
  • 2011-08-05
  • 2010-10-24
  • 2011-08-28
相关资源
最近更新 更多