문제

I'm trying to create a POST request with Tsung

<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?

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

is of course invalid...

Is there a way I can send JSON with my POST request?

도움이 되었습니까?

해결책

I was able to do this by replacing " &quot; and adding the content type header 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. 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.

Then I find the IP address of one of the packets select the 'Analyze' menu and select 'Follow TCP Stream'.

This allows me to see the actual data sent.

The data can look something like this:

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'

Then I modify the tsung xml to look like:

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top