R POST() 状态 422 错误(消息"值不是有效列表")



我无法在r中POST() API,我做了以下操作:

data  <- list(request_data_type = "expression",
request_cancer_type = "all",
request_genes = c("BRCA1", "PALB2", "SRY", "TP53", "NOTCH1"),
request_models = c("CTG-0009", "CTG-0011", "CTG-0012"),
request_dataset = "PDX",
request_key = "XXX",
request_client = 99,
request_user = 99,
request_mode = 'true') 
request  <-  POST(url = 'https://example.com/workstation', 
body = data)
request

消息是

Response [https://example.com/workstation]
Date: 2021-10-11 15:33
Status: 422
Content-Type: application/json
Size: 116 B

我无法获取状态200.

使用Python提取数据没有问题:

import requests
import pandas as pd
data = {
"request_data_type": "expression",
"request_cancer_type": ["all"],
"request_genes": ["BRCA1", "PALB2", "SRY", "TP53", "NOTCH1"],
"request_models": ["CTG-0009", "CTG-0011", "CTG-0012"],
"request_dataset": "PDX",
"request_key": "XXX",
"request_client": 99,
"request_user": 99,
"request_mode": 'true'
}
response = requests.post('https://example.com/workstation', json=data) # this saves a .json file in the directory
df = pd.read_json('../<file_name>.json')
df.head(2)

给出了预期的结果:("这dataframe"

您似乎已经从R代码中编辑了encode='json'部分。这一点很重要,因为您将在python脚本中通过json发送数据。唯一的另一个区别是,你似乎明显是在"拳击"。"request_cancer_type"Value在python代码中是一个数组,而不是一个简单的字符串值。你可以在R中用list()包装这个值。这为我生成了与python代码相同的请求:

data  <- list(request_data_type = "expression",
request_cancer_type = list("all"),
request_genes = c("BRCA1", "PALB2", "SRY", "TP53", "NOTCH1"),
request_models = c("CTG-0009", "CTG-0011", "CTG-0012"),
request_dataset = "PDX",
request_key = "XXX",
request_client = 99,
request_user = 99,
request_mode = 'true') 
request  <-  POST(url = 'https://example.com/workstation', 
body = data, encode='json')