使用MEXC API下止损限价单遇无效内容类型错误(700013)求助
问题
尝试使用MEXC API下达止损限价单时持续报错,错误信息如下:
HTTP error occurred: 400 Client Error: Bad Request for url: https://api.mexc.com/api/v3/order
{"code":700013,"msg":"Invalid content Type."}
使用的下单脚本:
import requests import time import hmac import hashlib # Your MEXC API key and secret API_KEY = 'your_api_key' API_SECRET = 'your_api_secret' # Base URL for MEXC API BASE_URL = 'https://api.mexc.com/api/v3' # Function to create the signature for the request def create_signature(query_string, secret): return hmac.new(secret.encode(), query_string.encode(), hashlib.sha256).hexdigest() # Function to place a stop-limit order def place_stop_limit_order(): endpoint = '/order' timestamp = int(time.time() * 1000) # Parameters for the stop-limit order params = { 'symbol': 'ETHUSDC', 'side': 'BUY', 'type': 'STOP_LOSS_LIMIT', # Order type 'quantity': '0.01', # Quantity to buy 'price': '4000.01', # Limit price 'stopPrice': '4000', # Stop price 'timeInForce': 'GTC', # Time in force 'timestamp': timestamp } query_string = '&'.join([f"{key}={value}" for key, value in sorted(params.items())]) signature = create_signature(query_string, API_SECRET) headers = { 'X-MEXC-APIKEY': API_KEY, 'Content-Type': 'application/x-www-form-urlencoded' # Correct content type } # Adding signature to the query string query_string += f"&signature={signature}" # Make the request to place the order response = requests.post(BASE_URL + endpoint, headers=headers, data=query_string) try: response.raise_for_status() # Raise an HTTPError for bad responses (4xx and 5xx) except requests.exceptions.HTTPError as err: print(f"HTTP error occurred: {err}") # Print HTTP error print(response.text) # Print the response content except Exception as err: print(f"Other error occurred: {err}") # Print other errors return response.json() # Main function to place the stop-limit order def main(): result = place_stop_limit_order() print(f"Order placed: {result}") if __name__ == "__main__": main()
已按文档要求设置Content-Type为application/x-www-form-urlencoded,但仍收到无效内容类型错误,需排查问题并给出修正建议。
问题排查与修正建议
核心问题分析
脚本存在两个关键问题:
- 请求参数传递方式错误:MEXC POST接口要求参数以字典形式传入
data参数,而非拼接成字符串直接传入。直接传字符串时,requests库可能自动修改Content-Type,导致服务器识别错误。 - 手动拼接字符串易引发编码问题:手动拼接查询字符串可能出现参数编码不规范的情况,进一步加剧内容类型识别错误。
修正后的脚本
import requests import time import hmac import hashlib # Your MEXC API key and secret API_KEY = 'your_api_key' API_SECRET = 'your_api_secret' # Base URL for MEXC API BASE_URL = 'https://api.mexc.com/api/v3' # Function to create the signature for the request def create_signature(params, secret): # 按key排序参数并拼接成查询字符串 query_string = '&'.join([f"{key}={value}" for key, value in sorted(params.items())]) return hmac.new(secret.encode(), query_string.encode(), hashlib.sha256).hexdigest() # Function to place a stop-limit order def place_stop_limit_order(): endpoint = '/order' timestamp = int(time.time() * 1000) # Parameters for the stop-limit order params = { 'symbol': 'ETHUSDC', 'side': 'BUY', 'type': 'STOP_LOSS_LIMIT', 'quantity': '0.01', 'price': '4000.01', 'stopPrice': '4000', 'timeInForce': 'GTC', 'timestamp': timestamp } # 生成签名并加入参数列表 signature = create_signature(params, API_SECRET) params['signature'] = signature headers = { 'X-MEXC-APIKEY': API_KEY, 'Content-Type': 'application/x-www-form-urlencoded' } # 直接传入参数字典,requests自动处理为正确的表单格式 response = requests.post(BASE_URL + endpoint, headers=headers, data=params) try: response.raise_for_status() except requests.exceptions.HTTPError as err: print(f"HTTP error occurred: {err}") print(response.text) except Exception as err: print(f"Other error occurred: {err}") return response.json() # Main function to place the stop-limit order def main(): result = place_stop_limit_order() print(f"Order placed: {result}") if __name__ == "__main__": main()
关键修改说明
- 参数传递方式调整:将拼接好的查询字符串改为字典传入
data参数,requests会自动将字典编码为符合application/x-www-form-urlencoded格式的内容,确保服务器正确识别。 - 签名生成逻辑优化:直接接收参数字典生成签名,避免手动拼接字符串可能出现的编码错误,代码更简洁可靠。
- 参数完整性保障:所有参数(包括签名)以字典形式统一传递,避免遗漏或格式偏差。
内容的提问来源于stack exchange,提问作者user313215
相关产品推荐
相关产品推荐

