Search code examples
pythonpython-requestsdwr

post request python cross-domain to DWR server


i want post request to dwr server with some payload date like this:

callCount=1
c0-scriptName=Manager
c0-id=3992_1389451476736
c0-param1=string:
c0-param15=string:
c0-param16=string:-1
c0-param17=string:1
xml=true

i use requests python my first questions is I do not know what kind of data accept dwr (string, json or ...) my code is

url = 'https://example.com/Manage.dwr'
headers = {
}

data = 'callCount=1c0-scriptName=MMTPManagerc0-methodName=saveMMTPOrderc0-id=7140_1389449863325c0-param0=string:072CD0A2FE4DAD0450457485587ADB5Cc0-param1=string:c0-param2=string:IRO1MAPN0001c0-param3=string:12190060428937%20%20c0-param4=string:Ac0-param5=string:20000c0-param6=string:0c0-param7=string:c0-param8=string:12913c0-param9=string:Lc0-param10=string:Jc0-param11=string:c0-param12=number:758944325c0-param13=string:2c0-param14=number:1389448044227c0-param15=string:c0-param16=string:-1c0-param17=string:1xml=true'



def req():
    r = requests.post(url, data=data, headers=headers)
    print r.text
    print r.headers
req()

and response is

//<script type='text/javascript'>
alert('Error. This may be due to an unsupported browser.\nSee the mailing lists at http://www.getahead.ltd.uk/dwr/ for more information.');
//</script>

for every idea and help Grateful thanks


Solution

  • Having never heard of dwr before, I took a quick look and found the Direct Web Remoting website. Since it looks to require something that understands JavaScript on the client, you cannot use requests alone.

    requests does not do any processing of the contents of the webpage. Since requests does not handle JavaScript for you, you'll need to use something more like selenium (which has bindings in python).

    With something like selenium in the mix, you might need to change your User-Agent header in requests to something that looks like a real browser. Your request would look like:

    r = requests.post(url, data=data, headers={'User-Agent': '<browser user agent>'}
    

    Where '<browser user agent>' is a string taken from a browser. An example that might work can be

    Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0
    

    or

    Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36