【问题标题】:Python HTTP Request Returns 404 or BytesPython HTTP 请求返回 404 或字节
【发布时间】:2021-02-11 22:04:46
【问题描述】:

我正在尝试使用 Python 脚本来调用下面链接中详述的 API:

https://developer.wmata.com/docs/services/gtfs/operations/5cdc51ea7a6be320cab064fe?

当我使用下面的代码时,它总是返回一个 404 错误:

import requests
import json


def _url(path):
    return "http://api.wmata.com" + path


def pull_data():
    return requests.get(_url("/gtfs/bus-gtfsrt-tripupdates.pb"), params=params)


def jprint(obj):
    # create a formatted string of the Python JSON object
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)


# authenticate with your api key
params = {
    "apiKey": "mykey",
}

response = pull_data()
print(response)
jprint(response.json())

我也尝试使用链接中提供的python代码,但它返回无意义的响应内容,如下所示。任何解码内容的尝试均未成功。

Request-Context: appId=cid-v1:2833aead-1a1f-4ffd-874e-ef3a5ceb1de8
Cache-Control: public, must-revalidate, max-age=5
Date: Thu, 11 Feb 2021 22:05:31 GMT
ETag: 0x8D8CED90CC8419C
Content-Length: 625753
Content-MD5: fspEFl7LJ8QbZPgf677WqQ==
Content-Type: application/octet-stream
Expires: Thu, 11 Feb 2021 22:05:37 GMT
Last-Modified: Thu, 11 Feb 2021 22:04:49 GMT



1.0�Ԗ��

1818410080�


181841008020210211*52!�Ԗ�"19032("�Ԗ�"18173(#�Ԗ�"7779($�Ֆ�"18174(%�Ֆ�"7909(&�Ֆ�"7986('�Ֆ�"8039((�Ֆ�"8130()�֖�"8276(+�֖�"8313(,�ז�"8403(-�ז�"8452(.�ז�"8520(/�ؖ�"8604(1�ؖ�"8676(
7070 �Ӗ�(����������

1814174080�


181417408020210211*P129�Ԗ�"2373(:�Ԗ�"2387(;�Ԗ�"17296(=�Ֆ�"17212(>�֖�"2444(?�֖�"2493(@�֖�"2607(A�֖�"14633(B�֖�"2784(C�ז�"2832(D�ז�"2843(E�ז�"2848(F�ז�"2875(G�ؖ�"2945(H�ؖ�"2987(I�ؖ�"21946(K�ٖ�"14636(L�ٖ�"3122(M�ٖ�"3227(N�ٖ�"3308(O�ٖ�"3411(P�ٖ�"3500(Q�ٖ�"3539(R�ٖ�"14637(S�ږ�"3685(T�ږ�"15195(U�ږ�"15196(V�ۖ�"4243(W�ۖ�"4443(X�ۖ�"4517(Y�ۖ�"4631([�ܖ�"11962(
8002 �Ӗ�(/�

1825989080�


182598908020210211*7Y�Ӗ�"2158(�Ԗ�"2215(�Ԗ�"2259(�Ԗ�"2292(�Ԗ�"2299(�Ֆ�"18701(�Ֆ�"2310( �Ֆ�"2245(!�Ֆ�"2174("�Ֆ�"1987(#�֖�"1937(%�֖�"1864(
3191 �Ӗ�(��


1819988080�

任何指导或方向将不胜感激!

【问题讨论】:

    标签: python python-requests httprequest gtfs


    【解决方案1】:

    改成pull_data函数如下:

    def pull_data():
        return requests.get(_url("/gtfs/bus-gtfsrt-tripupdates.pb"), headers=headers)
    

    然后将params模块全局变量重命名为headers

    headers = {"apiKey": "mykey"}
    

    WMATA 在标头中查找 apiKey ,而不是在查询参数中。

    更新:我注意到他们对一些样本使用api_key,而对另一些样本使用apiKey。例如参见: https://developer.wmata.com/docs/services/gtfs/operations/5cdc51ea7a6be320cab064fe

    更新 2:注意响应标头中的内容类型:

    print(response.headers['content-type'])
    # application/octet-stream
    

    它不是 JSON。获取内容如下:

    print(response.content)
    

    工作示例:

    import requests
    
    API_URL = 'https://api.wmata.com'
    
    
    def _prepare_url(path):
        return f'{API_URL}/{path.lstrip("/")}'
    
    
    def pull_data(**options):
        url = _prepare_url('/gtfs/bus-gtfsrt-tripupdates.pb')
        return requests.get(url, **options)
    
    
    response = pull_data(headers={'api_key': 'secret'})
    print(response.content)
    

    【讨论】:

    • 感谢您的帮助,但我仍然收到与您的更改相同的 404 响应。
    • @Mike 我注意到他们对一些样本使用api_key,对另一些样本使用apiKey。你能试试api_key吗?
    • 好的,使用 api_key 结合@MCcodemasher 的建议返回成功的 200 响应!但是,response.json() 返回以下错误:“JSONDecodeError: Expecting value: line 3 column 1 (char 3)。”这可能与它是一个应用程序/八位字节流有关吗?
    • json.loads(response) 返回此错误:“TypeError: JSON object must be str, bytes or bytearray, not Response”
    • response.content 返回一个无意义的流:"b'\n\r\n\x031.0\x10\x00\x18\xf9\xb9\x9a\x81\x06\x12\xc6\ x03\n\n1832238090\x1a..."
    【解决方案2】:

    尝试更改您的 URL,使其使用 https 而不是 http。您在 Bus RT Trip Updates 链接的文档似乎表明需要 https。

    改变这个:

    def _url(path):
    return "http://api.wmata.com" + path
    

    做到这一点:

    def _url(path):
    return "https://api.wmata.com" + path
    

    【讨论】:

    • 感谢您的帮助,但我收到了 401 响应消息“由于缺少订阅密钥而拒绝访问。请确保在向 API 发出请求时包含订阅密钥。”但是,我尝试在标头和查询参数中都包含 API 密钥,但没有成功。
    猜你喜欢
    • 2017-04-19
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多