Sending json with tsung POST request
Asked Answered
C

3

5

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?

Cundiff answered 8/5, 2014 at 12:23 Comment(0)
C
7

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;}
Cundiff answered 20/5, 2014 at 8:22 Comment(0)
T
1

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" 
/>
Terzetto answered 24/2, 2020 at 20:59 Comment(0)
T
0

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.

Tyishatyke answered 9/5, 2014 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.