2

I have a data

data = '{"account_id":2004,"email_address":"mhn34@benu.com","password":"benu123","account_type":"customer","name_prefix":"","first_name":"MHN","middle_names":"","last_name":"User","name_suffix":"","non_person_name":false,"DBA":"","display_name":"MHNUser","address1":"75 Saint Alphonsus Street","address2":"","address3":"","city":"Boston","state":"MA","postal_code":"02120","nation_code":"USA","phone1":"8127085695","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":"1"}'

print data


url = 'http://172.16.139.130:1234/vse/account'
r = requests.post(url, data=data)
print r.text
print r.status_code

I'm trying to make a POST, I kept getting

Traceback (most recent call last):
  File "create_account2.py", line 61, in <module>
    r = requests.post(url, data=data)
  File "/Library/Python/2.7/site-packages/requests/api.py", line 107, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/api.py", line 53, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/adapters.py", line 426, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))

What did I do wrong ?


Try #2

But if I run this 

curl -X POST -d '{"account_id":2004,"email_address":"mhn34@benu.com","password":"benu123","account_type":"customer","name_prefix":"","first_name":"MHN","middle_names":"","last_name":"User","name_suffix":"","non_person_name":false,"DBA":"","display_name":"MHNUser","address1":"75 Saint Alphonsus Street","address2":"","address3":"","city":"Boston","state":"MA","postal_code":"02120","nation_code":"USA","phone1":"8127085695","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":"1"}' http://172.16.139.130:1234/vse/account

I got

**{"status":200,"message":"Success","data":{}}**

@Ni. Here is the stream content of my curl `POST /vse/account HTTP/1.1

Host: 172.16.139.130:1234
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 482
Content-Type: application/x-www-form-urlencoded

{"account_id":2004,"email_address":"mhn34@benu.com","password":"benu123","account_type":"customer","name_prefix":"","first_name":"MHN","middle_names":"","last_name":"User","name_suffix":"","non_person_name":false,"DBA":"","display_name":"MHNUser","address1":"75 Saint Alphonsus Street","address2":"","address3":"","city":"Boston","state":"MA","postal_code":"02120","nation_code":"USA","phone1":"8127085695","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":"1"}HTTP/1.1 200 OK
Content-Length: 44
Date: Sat, 30 Apr 2016 18:55:44 GMT

{"status":200,"message":"Success","data":{}}`

Try #3

data = {"account_id":2004,"email_address":"mhn34@benu.com","password":"benu123","account_type":"customer","name_prefix":"","first_name":"MHN","middle_names":"","last_name":"User","name_suffix":"","non_person_name":bool("false"),"DBA":"","display_name":"MHNUser","address1":"75 Saint Alphonsus Street","address2":"","address3":"","city":"Boston","state":"MA","postal_code":"02120","nation_code":"USA","phone1":"8127085695","phone2":"","phone3":"","time_zone_offset_from_utc":-5,"customer_type":"1"}

url = 'http://172.16.139.130:1234/vse/account'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'}
r = requests.post(url, headers=headers, data=data)

print(r.status_code, r.reason)

I got

(400, 'Bad Request')
halfer
  • 19,824
  • 17
  • 99
  • 186
code-8
  • 54,650
  • 106
  • 352
  • 604

2 Answers2

4

Seems like there are a few possible reasons for this. In general, the server decides to reject your request based on its headers. It might be connected to the user agent; the server might recognize that you are not using a standard browser and wants to prevent scraping or any other kind of programmatic access. Another option is that you may be trying to connect with a wrong protocol (for example http instead of https).

To figure out what exactly is happening you can try using wireshark or other similar tools to inspect the communication between you and the server.

Read more in these links:

Update: It seems more likely that your connection is rejected due to your user-agent. cURL can have a browser-like user-agent, and it is not being rejected. Try setting your user agent to something browser-like:

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'}
r = requests.post(url, data=data, headers=headers)

Update 2:

First, here is a difference between the cURL parameters and the python parameters: in python there's "non_person_name":bool("false"), and in cURL just "non_person_name":"false".

Second, try adding those headers to your python request:

Host: 172.16.139.130:1234
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 482
Content-Type: application/x-www-form-urlencoded

Maybe specifying Content-Length it a bad idea, so remove it from your headers first if it doesn't work.

Community
  • 1
  • 1
kmaork
  • 5,722
  • 2
  • 23
  • 40
0

The most likely solution seems to be that the remote site is determining from your request that it doesn't come from a human using a web browser, and disconnection without sending an HTTP response of any kind. The only solution would be to add sufficient header information to your request to fake a "proper" request that the remote server is prepared to accept.

You might also wish to note that the problem could be caused by your passing a string rather than a dict to the post request. As this requests documentation shows, a dict is expected. You can achieve the required transformation by removing the quotes surrounding the value you bind to data in the first line of your code.

holdenweb
  • 33,305
  • 7
  • 57
  • 77