0

I want to make POST data with requests in Python.

This is the data, actually, I grab from Burp Suite (intercept HTTP request).

sessionid=xxxsesi&serverid=1&partner=xxxpartner&tradeoffermessage=&json_tra
deoffer={"newversion":true,"version":3,"me":{"assets":
[{"appid":0,"contextid":"2","amount":1,"assetid":"xxxasset"}],"currency":
[],"ready":false},"them":{"assets":[],"currency":
[],"ready":"false"}}&captcha=&trade_offer_create_params=
{"trade_offer_access_token":"xxxtoken"}

But if I try to write in requests POST Python, it always fails.

data = {
"sessionid": "xxxsession",
"serverid": 1,
"partner":"xxxpartner",
"tradeoffermessage": "",
"json_tradeoffer": {"newversion":True,"version":3,"me":{"assets":[{"appid":440,"contextid":"2","amount":1,"assetid":"xxxasset"}],"currency":[],"ready":False},"them":{"assets":[],"currency":[],"ready":False}},
"captcha": "",
"trade_offer_create_params": {"trade_offer_access_token":"xxxtoken"}
}

Can someone help me? Sorry for my bad English.

0xCursor
  • 2,242
  • 4
  • 15
  • 33
  • The JSON in the payload should be form encoded. The request you intercepted is not well-formed. It is possible that `requests` does that for you but the serer actually expects the faulty version. You might have to construct the payload (`data`) manually and pass it in as a string. – Klaus D. Jul 28 '18 at 04:59
  • perhaps duplicated. – Benyamin Jafari Jul 28 '18 at 07:40

1 Answers1

3

In Python post request is more complicated as compare to other requests.but here is the solution for post request in python.

Example#1: POST REQUEST

  >>> payload = {'key1': 'value1', 'key2': 'value2'}

>>> r = requests.post("http://httpbin.org/post", data=payload)
>>> print(r.text)
{
  ...
  "form": {
    "key2": "value2",
    "key1": "value1"
  },
  ...
}

Example#2: POST REQUEST

>>> import requests
>>> r = requests.post('http://httpbin.org/post', json={"key": "value"})
>>> r.status_code
200
>>> r.json()
{'args': {},
 'data': '{"key": "value"}',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
             'Accept-Encoding': 'gzip, deflate',
             'Connection': 'close',
             'Content-Length': '16',
             'Content-Type': 'application/json',
             'Host': 'httpbin.org',
             'User-Agent': 'python-requests/2.4.3 CPython/3.4.0',
             'X-Request-Id': 'xx-xx-xx'},
 'json': {'key': 'value'},
 'origin': 'x.x.x.x',
 'url': 'http://httpbin.org/post'}

BUT IN YOUR SCENARIO

you're using the burp suite to get data and post data in python (I don't know which specific version of python are you using) but you should follow basic method of post request in python.if you're using the brup suite and must read the brup suite documentation

Here is the documentation link which'll explain you each step one by one and do keep an eye on the notes and TODOs while following the step (in your scenario).

  • i understant about post data in requests python, if data just some string or integer. example : data=a&data2=b datapost = {'data': 'a', 'data2': 'b'} but if post data is json, i dont know, how to write it. can u explain write, how to write it ? – Bahtiyar Istiyarno Jul 28 '18 at 12:37
  • Simply do like this.requests.post(url, headers=headers, data=data There are many times that you want to send data that is not form-encoded. If you pass in a string instead of a dict, that data will be posted directly. – Muhammad Umair Ghufran Jul 28 '18 at 12:40