我在Python 3上使用urllib.request
发送JSON。
data = {"a": "1"}
req = urllib.request.Request('https://example.com', data=json.dumps(data).encode('utf8'), headers={'Content-Type': 'application/json'})
urllib.request.urlopen(req)
问题是data=json.dumps(data).encode('utf8')
将{"a": "1"}
转换为具有b
前缀b'{"a": "1"}'
的相同字符串。
我知道在python中,我可以使用decode('utf8)
来删除b
前缀,但我需要能够在服务器端做到这一点,因为python 3强制您发送字节流数据。
我使用php作为服务器端代码
我试过使用utf8_decode()
,但它没有任何作用。
如何删除服务器端代码上的b
前缀?
服务器端是PHP对吗?您可以尝试使用utf8_decode()
函数。试着看看这个函数是否能解决你的问题。
$data = utf8_decode($data_from_python);
看看这个:http://php.net/manual/en/function.utf8-decode.php