我有数据
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
我想发个帖子,我一直在
^{pr2}$我做错什么了?在
如有任何提示/建议,我们将不胜感激!在
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
我得到了
**{"status":200,"message":"Success","data":{}}**
@Ni。下面是mycurl`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":{}}`
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)
我得到了
(400, 'Bad Request')
似乎有几个可能的原因。一般来说,服务器决定根据其头拒绝您的请求。它可能连接到用户代理;服务器可能会识别出您没有使用标准浏览器,并希望防止抓取或任何其他类型的编程访问。另一个选择是,您可能试图使用错误的协议(例如http而不是https)进行连接。在
为了弄清楚到底发生了什么,可以尝试使用wireshark或其他类似的工具来检查您和服务器之间的通信。在
在这些链接中阅读更多信息:
更新:由于用户代理,连接被拒绝的可能性更大。cURL可以有一个类似浏览器的用户代理,它不会被拒绝。尝试将您的用户代理设置为浏览器,如:
更新2:
首先,这里是cURL参数和python参数之间的区别: 在python中有
"non_person_name":bool("false")
,在cURL中只有"non_person_name":"false"
。在其次,尝试将这些头添加到python请求中:
^{pr2}$也许指定
Content-Length
是个坏主意,所以如果它不起作用,请先从头中删除它。在最有可能的解决方案似乎是,远程站点根据您的请求确定它不是来自使用web浏览器的人类,并且在不发送任何类型的HTTP响应的情况下断开连接。唯一的解决方案是向请求添加足够的头信息,以伪造远程服务器准备接受的“正确”请求。在
您可能还希望注意,问题可能是由于您向
post
请求传递了一个字符串而不是dict。如requests documentation所示,需要dict。通过删除代码第一行中绑定到data
的值的引号,可以实现所需的转换。在相关问题 更多 >
编程相关推荐