Home > Blockchain >  R POST() Status 422 error (msg 'value is not a valid list')
R POST() Status 422 error (msg 'value is not a valid list')

Time:10-12

I am not able to POST() API in R. I did the following:

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

The message is

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

I cannot get a status 200.

I have no problem to pull the data using 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 gives the expected result: ["this dataframe"]

CodePudding user response:

When debugging stuff like this, https://httpreq.com/ is a helpful webpage to use. You can send both request to an endpoint there and compare the results.

You seem to have edited out the encode='json' part out of the R code. That's important since you are sending in the data via json in the python script. The only other difference is that you seem to be explicitly "boxing" the "request_cancer_type" value to be an array rather than a simple string value in the python code. You can do that in R by wrapping the value in a list(). This produces the same request as the python code for me:

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')

If that still doesn't work you'll need to provide some sort of documentation for exactly what format the API end point expects. Contact whomever runs that site because we can't really help further without knowing what code is running on that server.

  •  Tags:  
  • r api
  • Related