使用(例如)Chrome 的 DevTools,您可以看到进行了以下调用:
然后您可以将其复制为 cUrl 并在命令行上尝试执行此操作所需的标头:
$ curl 'https://admin.ucr.gov/api/enforcement' \
> -H 'authority: admin.ucr.gov' \
> -H 'accept: application/json, text/plain, */*' \
> -H 'cache-control: no-cache,no-store,must-revalidate,max-age=0,private' \
> -H 'ucr-ui-version: 20.5.4' \
> -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36' \
> -H 'dnt: 1' \
> -H 'content-type: application/json;charset=UTF-8' \
> -H 'origin: https://ucr.gov' \
> -H 'sec-fetch-site: same-site' \
> -H 'sec-fetch-mode: cors' \
> -H 'sec-fetch-dest: empty' \
> -H 'referer: https://ucr.gov/enforcement/1000511' \
> -H 'accept-language: it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7' \
> --data-binary '{"searchTerm":"1000511","itemsPerPage":15,"pageNumber":0}' \
> --compressed
{"carrier":{"usdot":1000511,"legalName":"877599 ALBERTA LTD","dateAdded":"2002-01-24T00:00:00Z","physicalAddress":{"street":"430 66 STREET SW","city":"EDMONTON","state":"AB","region":"CAAB","zipCode":"T6X 1A3","country":"C","countryCode":"CA"},"mailingAddress":{"street":"430 66 STREET SW","city":"EDMONTON","state":"AB","region":"CAAB","zipCode":"T6X 1A3","country":"C","countryCode":"CA"}},"history":{"enforcementRegistrations":[{"year":2020,"status":"unregistered","updateTime":"2020-06-15T16:12:53.5166421+00:00","isApplicable":true,"isYearActive":true,"updateTimeDisplay":"06/15/2020 16:12"},{"year":2019,"status":"unregistered","updateTime":"2020-06-15T16:12:53.53272+00:00","isApplicable":true,"isYearActive":true,"updateTimeDisplay":"06/15/2020 16:12"},{"year":2018,"status":"unregistered","updateTime":"2020-06-15T16:12:53.5486724+00:00","isApplicable":true,"isYearActive":false,"updateTimeDisplay":"06/15/2020 16:12"},{"year":2017,"status":"unregistered","updateTime":"2020-06-15T16:12:53.5646021+00:00","isApplicable":true,"isYearActive":false,"updateTimeDisplay":"06/15/2020 16:12"}]}}
现在你可以尝试一个一个的去掉headers,你会发现这个请求成功了:
$ curl 'https://admin.ucr.gov/api/enforcement' --data-binary '{"searchTerm":"1000511"}' -H 'ucr-ui-version: 20.5.4' -H 'content-type: application/json;charset=UTF-8'
{"carrier":{"usdot":1000511,"legalName":"877599 ALBERTA LTD","dateAdded":"2002-01-24T00:00:00Z","physicalAddress":{"street":"430 66 STREET SW","city":"EDMONTON","state":"AB","region":"CAAB","zipCode":"T6X 1A3","country":"C","countryCode":"CA"},"mailingAddress":{"street":"430 66 STREET SW","city":"EDMONTON","state":"AB","region":"CAAB","zipCode":"T6X 1A3","country":"C","countryCode":"CA"}},"history":{"enforcementRegistrations":[{"year":2020,"status":"unregistered","updateTime":"2020-06-15T16:20:41.3271743+00:00","isApplicable":true,"isYearActive":true,"updateTimeDisplay":"06/15/2020 16:20"},{"year":2019,"status":"unregistered","updateTime":"2020-06-15T16:20:41.3951487+00:00","isApplicable":true,"isYearActive":true,"updateTimeDisplay":"06/15/2020 16:20"},{"year":2018,"status":"unregistered","updateTime":"2020-06-15T16:20:41.468421+00:00","isApplicable":true,"isYearActive":false,"updateTimeDisplay":"06/15/2020 16:20"},{"year":2017,"status":"unregistered","updateTime":"2020-06-15T16:20:41.5511652+00:00","isApplicable":true,"isYearActive":false,"updateTimeDisplay":"06/15/2020 16:20"}]}}
现在将curl 调用转换为python。请注意,调用是 POST 而不是您的代码中的 GET!:
In [1]: import requests
In [2]: import io
...: response = requests.post('https://admin.ucr.gov/api/enforcement', data=io.StringIO('{"searchTerm":"1000511"}'), headers={'ucr-ui-version': '20.5.4', 'content-type': 'application/json;charset=UTF-8'})
In [3]: response.status_code
Out[3]: 200
In [4]: response.json()
Out[4]:
{'carrier': {'usdot': 1000511,
'legalName': '877599 ALBERTA LTD',
'dateAdded': '2002-01-24T00:00:00Z',
'physicalAddress': {'street': '430 66 STREET SW',
'city': 'EDMONTON',
'state': 'AB',
'region': 'CAAB',
'zipCode': 'T6X 1A3',
'country': 'C',
'countryCode': 'CA'},
'mailingAddress': {'street': '430 66 STREET SW',
'city': 'EDMONTON',
'state': 'AB',
'region': 'CAAB',
'zipCode': 'T6X 1A3',
'country': 'C',
'countryCode': 'CA'}},
'history': {'enforcementRegistrations': [{'year': 2020,
'status': 'unregistered',
'updateTime': '2020-06-15T16:23:10.2114276+00:00',
'isApplicable': True,
'isYearActive': True,
'updateTimeDisplay': '06/15/2020 16:23'},
{'year': 2019,
'status': 'unregistered',
'updateTime': '2020-06-15T16:23:10.278671+00:00',
'isApplicable': True,
'isYearActive': True,
'updateTimeDisplay': '06/15/2020 16:23'},
{'year': 2018,
'status': 'unregistered',
'updateTime': '2020-06-15T16:23:10.3507073+00:00',
'isApplicable': True,
'isYearActive': False,
'updateTimeDisplay': '06/15/2020 16:23'},
{'year': 2017,
'status': 'unregistered',
'updateTime': '2020-06-15T16:23:10.4026579+00:00',
'isApplicable': True,
'isYearActive': False,
'updateTimeDisplay': '06/15/2020 16:23'}]}}