简体   繁体   English

使用 tsung POST 请求发送 json

[英]Sending json with tsung POST request

I'm trying to create a POST request with Tsung我正在尝试使用 Tsung 创建一个 POST 请求

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

How can I send a JSON document in the contents attribute?如何在内容属性中发送 JSON 文档?

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

is of course invalid...当然是无效的...

Is there a way I can send JSON with my POST request?有没有办法可以通过我的 POST 请求发送 JSON?

I was able to do this by replacing " &quot; and adding the content type header application/json我可以通过替换" &quot;并添加内容类型标题 application/json 来做到这一点

So my example would become:所以我的例子会变成:

content_type='application/json' contents='{&quot;name&quot;: &quot;alex&quot;}

The absolute simplest way (as of Tsung 1.3) is to make use of the contents_from_file attribute.最简单的方法(从 Tsung 1.3 开始)是使用contents_from_file属性。 That avoids polluting your code with a blob of escaped data and enables you to take any raw data and dump it in a file of your choosing这避免了大量转义数据污染您的代码,并使您能够获取任何原始数据并将其转储到您选择的文件中

<http url="mypage" 
      method="POST" 
      content_type='application/json'
      contents_from_file="/tmp/myfile.json" 
/>

When sending json, I usually capture a few successful requests in Wireshark without tsung running.发送json时,我一般是在Wireshark中捕获一些成功的请求,而没有运行tsung。

Then I find the IP address of one of the packets select the 'Analyze' menu and select 'Follow TCP Stream'.然后我找到其中一个数据包的 IP 地址,选择“分析”菜单并选择“遵循 TCP 流”。

This allows me to see the actual data sent.这使我可以看到发送的实际数据。

The data can look something like this:数据可能如下所示:

data.text=US+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'

Then I modify the tsung xml to look like:然后我将 tsung xml 修改为:

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

And paste the data in the contents.并将数据粘贴到内容中。

You could also try escaping the quotes using " but I never had any luck.您也可以尝试使用 " 转义引号,但我从来没有运气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM