2

I've recently been getting a 400 bad request parse error when making a request to my timeline_url.

Im posting to this url timeline_url = 'https://www.googleapis.com/mirror/v1/timeline'

EDIT Here is the code:

    headers = {'Content-Type': 'application/json',
               'Authorization' : 'Bearer %s' % access_token}
    body = {"text" : "Waddup doe!"}
    """ ***********THIS IS RETURNING ERROR 400 PARSE ERROR***********"""
    send_request(timeline_url, headers, 'POST',body)

The send_request method is using requests

req = requests.post(url, data=body, headers=headers)

I'm just trying to insert plain text to my timeline.

4

1 回答 1

3
body_bytes = sys.getsizeof(body)
headers['Content-Length'] = body_bytes

这是在您的请求标头中插入不正确的值。sys.getsizeof描述了数据结构有多大——包括指针、计数器等。它没有描述字符串表示在 HTTP 流上占用了多少字节。

只需删除这些行;requestsContent-Length自动填写。


您没有描述如何对有效负载进行 json 编码。也许你需要这样做:

req = requests.post(url, data=json.dumps(body), headers=headers)

请参阅:http ://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests

于 2013-07-29T15:38:52.007 回答