使用请求的Python HTTP请求二进制数据

Sha*_*pal 3 python curl http url-parameters python-requests

以下代码仅适用于curl.如果你能告诉我为什么它不能使用Requests在Python中运行会很好

curl 'http://cdcnepal.com/Modules/HOmeMoviesLists/WebService2.asmx/GetShowsByDate' \
 -H 'Content-Type: application/json; charset=UTF-8' \
 -d '{"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}'
Run Code Online (Sandbox Code Playgroud)

但是在Python中使用以下代码

import requests
import json
url = """http://cdcnepal.com/Modules/HOmeMoviesLists/WebService2.asmx/GetShowsByDate"""
headers = {"content-type":["application/json", "charset=UTF-8"]}
payload = {"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}
r = requests.get(url, headers=headers, data=payload)
print r.text
Run Code Online (Sandbox Code Playgroud)

最初卷曲请求有其他内容,下面,但我意识到我可以删除几个.我不确定是否导致错误,因为curl请求正在运行.我没有从代码中获得相同的响应.

这可能很有用.从Chrome开发工具中提取的卷曲请求

curl 'http://cdcnepal.com/Modules/HOmeMoviesLists/WebService2.asmx/GetShowsByDate'
 -H 'Cookie: OriginalReferrer=https://www.google.com/;
     OriginalURL=http://cdcnepal.com/;
     ASP.NET_SessionId=i5lbnql5hpp0wm1ywyqbywtj;
     VisitCount=4' 
 -H 'Origin: http://cdcnepal.com' 
 -H 'Accept-Encoding: gzip,deflate,sdch' 
 -H 'Accept-Language: en-US,en;q=0.8,hi;q=0.6' 
 -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3)
     AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36' 
 -H 'Content-Type: application/json; charset=UTF-8' 
 -H 'Accept: application/json, text/javascript, */*; q=0.01' 
 -H 'Referer:http://cdcnepal.com/Home.aspx' 
 -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' 
 -H 'DNT: 1' 
 --data-binary '{"portalId":"1","showDate":"27/05/2014","flag":0,"size":9}' --compressed
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 10

curl -d交换机发送一个POST请求,但使用的是requests.get()相反,发送GET请求(的尸体被忽略).

通过使用request.post()以下方式使其成为POST :

import requests
import json

url = "http://cdcnepal.com/Modules/HOmeMoviesLists/WebService2.asmx/GetShowsByDate"
headers = {"content-type": "application/json; charset=UTF-8"}
payload = {"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}
r = requests.post(url, headers=headers, data=json.dumps(payload))
print r.text
Run Code Online (Sandbox Code Playgroud)

你还需要:

  1. 不使用content-type标题列表,不支持单独指定的参数.
  2. 将JSON数据编码为JSON字符串; requests不会为你这样做.相反,传递给的字典data被编码为application/x-www-form-urlencoded数据.

您可以使用http://httpbin.org/post更轻松地比较curl命令:requests

$ curl http://httpbin.org/post \
>  -H 'Content-Type: application/json; charset=UTF-8' \
>  -d '{"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}'

{
  "args": {},
  "data": "{\"portalId\":\"1\",\"showDate\":\"26/05/2014\",\"flag\":0,\"size\":9}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Connection": "close",
    "Content-Length": "58",
    "Content-Type": "application/json; charset=UTF-8",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.30.0",
    "X-Request-Id": "78d7bb7d-e29b-482b-908a-48d2395a050f"
  },
  "json": {
    "flag": 0,
    "portalId": "1",
    "showDate": "26/05/2014",
    "size": 9
  },
  "origin": "84.92.98.170",
  "url": "http://httpbin.org/post"
}
Run Code Online (Sandbox Code Playgroud)

>>> import requests
>>> import json
>>> from pprint import pprint
>>> url = 'http://httpbin.org/post'
>>> headers = {"content-type":"application/json; charset=UTF-8"}
>>> payload = {"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}
>>> r = requests.post(url, headers=headers, data=json.dumps(payload))
>>> pprint(r.json())
{u'args': {},
 u'data': u'{"portalId": "1", "flag": 0, "size": 9, "showDate": "26/05/2014"}',
 u'files': {},
 u'form': {},
 u'headers': {u'Accept': u'*/*',
              u'Accept-Encoding': u'gzip, deflate, compress',
              u'Connection': u'close',
              u'Content-Length': u'65',
              u'Content-Type': u'application/json; charset=UTF-8',
              u'Host': u'httpbin.org',
              u'User-Agent': u'python-requests/2.2.1 CPython/2.7.6 Darwin/13.1.0',
              u'X-Request-Id': u'06d6b542-c279-4898-8701-2c0d502aa36e'},
 u'json': {u'flag': 0,
           u'portalId': u'1',
           u'showDate': u'26/05/2014',
           u'size': 9},
 u'origin': u'84.92.98.170',
 u'url': u'http://httpbin.org/post'}
Run Code Online (Sandbox Code Playgroud)

两种情况都显示json返回相同的字典.

如果您使用的是requests2.4.2或更高版本,则还可以将JSON编码保留到库中; 如果您传入要作为json关键字参数发送的数据,它也会设置正确的Content-Type标头:

import requests

url = "http://cdcnepal.com/Modules/HOmeMoviesLists/WebService2.asmx/GetShowsByDate"
payload = {"portalId":"1","showDate":"26/05/2014","flag":0,"size":9}
r = requests.post(url, json=payload)
print r.text
Run Code Online (Sandbox Code Playgroud)