1

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"]

JAM
  • 21
  • 4
  • Every API is different. Without reading the exact documentation for what type of request they expect, it's not really possible to say what could be wrong. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Do they give examples of what a proper request should look like? – MrFlick Oct 11 '21 at 16:29
  • I cannot post the access key here, but I have updated the original post with the python code, which can pull the data correctly. The expected result is at the end link "this dataframe". – JAM Oct 11 '21 at 18:50

1 Answers1

0

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')
MrFlick
  • 178,638
  • 15
  • 253
  • 268