replicate curl command python 3 urllib request API -


this problem kind of driving me crazy. i'm doing simple python 3 script manage api in public website. able curl, not in pyhton. can't use either requests library or curl in real environment, tests

this working:

curl -d "credential_0=xxxx&credential_1=xxxxxx" -c cookiefile.txt https://xxxxxxxxxxxxxxx/login  curl -d 'json={"devices" : ["00:1a:1e:29:73:b2","00:1a:1e:29:73:b2"]}' -b cookiefile.txt -v https://xxxxxxxxx/api-path --trace-ascii /dev/stdout 

and can see in curl debug:

send header, 298 bytes (0x12a)

0000: post /api-path http/1.1

0034: host: xxxxxxxxxxxxxxxx

0056: user-agent: curl/7.47.0

006f: accept: /

007c: cookie: csrf_token=751b6bd9-0290-496b-820e-xxxxxxxx; session 00bc: =xxxxxx-6d29-4cf9-8907-xxxxxxxxxxxx

00e3: content-length: 60

00f7: content-type: application/x-www-form-urlencoded

0128: => send data, 60 bytes (0x3c)

0000: json={"devices" : ["00:1a:1e:29:73:b2","00:1a:1e:29:73:b2"]} == info: upload sent off: 60 out of 60 bytes

this python code replicate second request, problematic one

string_query={"devices" : [ "34:fc:b9:ce:14:7e","00:1a:1e:29:73:b2" ]} jsonbody_url=urllib.parse.urlencode(string_query) jsonbody_url=jsonbody_url.encode("utf-8")  req=urllib.request.request(url,data=jsonbody_url,headers={"cookie" :  cookie,"content-type": "application/x-www-form-urlencoded","user- agent":"curl/7.47.0","charset":"utf-8","content- length":len(jsonbody_url), "connection": "keep-alive"},method='post') 

and server ignoring json content. else working, login , other url parameters same api

any ideas?

try this:

import requests  string_query={"devices" : [ "34:fc:b9:ce:14:7e","00:1a:1e:29:73:b2" ]} headers={   "cookie" : cookie,   "content-type": "application/x-www-form-urlencoded",   "user-agent":"curl/7.47.0",   "charset":"utf-8",   "connection": "keep-alive" } response = requests.post(url,data=string_query,headers=headers) print(response.content) 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -