2017-08-09 10 views
0

JSONをAPIに投稿するためにリクエストモジュールを使用しているPythonスクリプトがあります。しかし、私は16進数を使用するハッシュを投稿しています。JSONの先頭にゼロが付いた文字列ですか?

r = requests.post('apiurl.com/do/stuff', json={"key": '0052ccca'}) 

応答が400エラーである:私は、私は次のコードを使用したときにエラーに実行しているよ

{"message": "Could not parse request body into json: Unrecognized token 
\'k0052ccca\': was expecting (\'true\', \'false\' or \'null\')\n at 
[Source: [[email protected]; line: 2, column: 23]"} 

this answerで勧告が文字列として先行ゼロを治療することであるが、しかし、私はすでにそれをやっており、まだエラーが発生しています。

答えて

4
>>> import requests 
>>> response = requests.post('http://httpbin.org/post', json={"key": '0052ccca'}) 
>>> print(response.text) 
{ 
    "args": {}, 
    "data": "{\"key\": \"0052ccca\"}", 
    "files": {}, 
    "form": {}, 
    "headers": { 
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "19", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.3" 
    }, 
    "json": { 
    "key": "0052ccca" 
    }, 
    "origin": "38.98.147.133", 
    "url": "http://httpbin.org/post" 
} 

あなたはそれが要求によって正しくエンコードされて見ることができるようjson、デコードは何も問題はありません。

>>> response.request.body 
b'{"key": "0052ccca"}' 

だから、問題はサーバ側である(または、あなたのサンプルコードは、あなたからあまりにも異なっています実際の問題を明らかにするための実際のコード)。

関連する問題