5

我正在尝试使用 Tsung 创建一个 POST 请求

<request> 
  <http url="api.whatever.com" method="POST" version="1.1" contents=""></http>
</request>

如何在内容属性中发送 JSON 文档?

contents='{"name": "alex"}' 

当然是无效的...

有没有办法可以通过我的 POST 请求发送 JSON?

4

3 回答 3

7

我可以通过替换" &quot;和添加内容类型标头 application/json来做到这一点

所以我的例子会变成:

content_type='application/json' contents='{&quot;name&quot;: &quot;alex&quot;}
于 2014-05-20T08:22:46.190 回答
1

最简单的方法(从 Tsung 1.3 开始)是使用contents_from_file属性。这样可以避免使用大量转义数据污染您的代码,并使您能够获取任何原始数据并将其转储到您选择的文件中

<http url="mypage" 
      method="POST" 
      content_type='application/json'
      contents_from_file="/tmp/myfile.json" 
/>
于 2020-02-24T20:59:15.910 回答
0

发送 json 时,我通常会在Wireshark中捕获一些成功的请求,而无需运行 tsung。

然后我找到其中一个数据包的 IP 地址,选择“分析”菜单并选择“跟踪 TCP 流”。

这让我可以看到发送的实际数据。

数据可能如下所示:

data.text=U.S.+intelligence+agents+have+been+hacking+computer+networks%0A++++++++++++++++++++++++around+the+world+for+years%2C+apparently+targeting+fat+data%0A++++++++++++++++++++++++pipes+that+push+immense+amounts+of+data+around+the+Internet%2C%0A++++++++++++++++++++++++NSA+leaker+Edward+Snowden+t&amp;type=text&amp;data.sender-id=8a5b1c2f-0589-464c-82c4-b8f4e280511a'

然后我将 tsung xml 修改为:

content_type='application/x-www-form-urlencoded' contents=''

并将数据粘贴到内容中。

您也可以尝试使用 " 转义引号,但我从来没有运气。

于 2014-05-09T18:44:18.870 回答