R POST()状态422错误(msg 'value is not a valid list')

yzckvree  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(155)

我不能在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)

这给出了预期的结果:[“this dataframe”]

rryofs0p

rryofs0p1#

您似乎已经编辑了R代码中的encode='json'部分。这一点很重要,因为你是通过python脚本中的json发送数据的。唯一的区别是,您似乎显式地将“request_cancer_type”值“装箱”为数组,而不是Python代码中的简单字符串值。在R中,可以通过将值 Package 在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')

相关问题