报错TypeError: memoryview: a bytes-like object is required, not 'str'

报错TypeError: memoryview: a bytes-like object is required, not 'str'

问题描述:


# coding=utf-8
import requests

url = "https://xxx:443/v1/sec/policy"
data = {
                ('device', '1'), ('vpn_user', '0'), (
                'sign', 'xxx'), (
                  'mid', 'xxx'), ('timestamp', '2021-10-28T10:21:37+08:00'), (
                  'app_name', 'xxx'), ('timezone', 'Asia/Shanghai'), ('format', 'JSON'), (
                  'campaign', 'default'), ('sign_version', '2.0.0'), ('sign_method', 'SHA256'), (
                  'appver', '2.0.3.1020'), ('lang', 'zh-hans'), ('version', '20201105'), (
                  'signnonce', '16353876973872634482'), ('channel', '0011'), ('imsi', ''), ('netop', ''), (
                  'lang_region', 'zh-CN'), ('region', 'CN')

}
response = requests.post(url=url, data=data)
print(response.text)

TypeError: memoryview: a bytes-like object is required, not 'str'

这该怎么改额

关键点:元组转字典

data = dict(data)

response = requests.post(url=url, data=data)
print(response.text)

或者

data = dict(data)

response = requests.post(url=url, json=data)
print(response.text)

改成下面再试试,欢迎来csdn技能树学习相关知识:


# coding=utf-8
import requests
import json
 
url = "https://xxx:443/v1/sec/policy"
data = {
        'device': '1', 
        'vpn_user': '0', 
        'sign': 'xxx',
        'mid': 'xxx',
        'timestamp': '2021-10-28T10:21:37+08:00',
        'app_name': 'xxx', 
        'timezone': 'Asia/Shanghai', 
        'format': 'JSON', 
        'campaign': 'default', 
        'sign_version': '2.0.0', 
        'sign_method': 'SHA256', 
        'appver': '2.0.3.1020', 
        'lang': 'zh-hans', 
        'version': '20201105', 
        'signnonce': '16353876973872634482', 
        'channel': '0011', 
        'imsi': '',
        'netop': '', 
        'lang_region': 'zh-CN',
        'region': 'CN'
}
data = json.dumps(data)
response = requests.post(url=url, data=data)

data = {
'login_email': login_email,
'login_password': login_password
}

data = json.dumps(dict(data))
response = requests.post(url=url, data=data)
print(response.text)

或者

response = requests.post(url=url, data=json.dumps(dict(data)))
print(response.text)