OKX接口报错请使用json格式

50006无效的Content_Type,请使用"application/json"格式

GET请求正常,POST请求格式签名正确,但请求报错

import requests,base64,hmac,datetime
pass_phrase = '***'        # 申请
api_key = "***"        # 申请
secret_key = "***"  # 申请

def GetTimeUTC():
    rest = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
    suffix = 'Z'
    rest = rest + suffix
    return rest

def signature(timestamp, method, request_path, body, secret_key):
  #签名
    body = {
        "instId":"BTC-USDT-SWAP",
        "tdMode":"cross",
        "side":"buy",
        "posSide":"long",
        "ordType":"market",
        "sz":"1"
    }
    message = str(timestamp) + str.upper(method) + request_path + str(body)
    mac = hmac.new(bytes(secret_key, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256')
    d = mac.digest()
    return base64.b64encode(d)

def get_header(api_key, current_time, pass_phrase, secret_key):
  #拼接请求头
    return {
      'OK-ACCESS-KEY': api_key,
      'OK-ACCESS-SIGN': signature(current_time, 'POST', '/api/v5/trade/order', 'None', secret_key),
      'OK-ACCESS-TIMESTAMP': current_time,
      'OK-ACCESS-PASSPHRASE': pass_phrase,
    }

timestamp = GetTimeUTC()
url = 'https://www.okex.com/api/v5/trade/order'
headers=get_header(api_key, timestamp, pass_phrase, secret_key)
response = requests.post(url=url, headers=headers).json()

使用post请求时,除了header外,还要根据需要传递其他参数。
示例:

当前接口的请求类型为application/json。

# 导入requests模块
import requests
# 请求的url地址
url = xxx
# 请求头
headers = {"content-type":"application/json"}
# payload 为传入的参数
payload = {"username":"vivi","password":"123456","remember_me":"false"}
# json形式,参数用json
res = requests.post(url,json=payload,headers=headers)
print(res.text)

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632