r-lib / httr

httr: a friendly http package for R
https://httr.r-lib.org
Other
986 stars 1.99k forks source link

POST request returns XML rather than JSON #720

Closed LiNk-NY closed 2 years ago

LiNk-NY commented 2 years ago

Original request via curl:

curl 'https://api.gdc.cancer.gov/files?size=1'

Using httr::POST with a simple query returns XML rather than JSON format, despite the Content-Type.

library(httr)
content(POST("https://api.gdc.cancer.gov/files?",
query = list(
    size=1
), encode = "json", add_headers(`Content-Type` = "application/json")
), as = "text")
#> No encoding supplied: defaulting to UTF-8.
#> [1] "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><response><data><hits><item><id>1294fc89-fae1-4d71-b970-dee321eeb6c1</id><data_format>VCF</data_format><access>controlled</access><file_name>9160d8a8-006d-4fd5-8c9e-09ec6179369a.wxs.muse.raw_somatic_mutation.vcf.gz</file_name><submitter_id>8c7fcd35-5fda-4eff-b368-f373fefc8327</submitter_id><data_category>Simple Nucleotide Variation</data_category><acl><item>phs000748</item></acl><type>simple_somatic_mutation</type><file_size>29042</file_size><state_comment></state_comment><created_datetime>2019-01-28T10:21:43.275213-06:00</created_datetime><md5sum>0138a8506fdbca4cf5b6c9e71c61f0b7</md5sum><updated_datetime>2019-06-24T08:07:19.797044-05:00</updated_datetime><file_id>1294fc89-fae1-4d71-b970-dee321eeb6c1</file_id><error_type></error_type><data_type>Raw Simple Somatic Mutation</data_type><state>released</state><experimental_strategy>WXS</experimental_strategy><version>1</version><data_release>18.0 - 34.0</data_release></item></hits><pagination><count>1</count><total>863977</total><size>1</size><from>0</from><sort></sort><page>1</page><pages>863977</pages></pagination></data><warnings></warnings></response>"
system2("curl", "'https://api.gdc.cancer.gov/files?size=1'", stdout = TRUE)
#> [1] "{\"data\": {\"hits\": [{\"id\": \"1294fc89-fae1-4d71-b970-dee321eeb6c1\", \"data_format\": \"VCF\", \"access\": \"controlled\", \"file_name\": \"9160d8a8-006d-4fd5-8c9e-09ec6179369a.wxs.muse.raw_somatic_mutation.vcf.gz\", \"submitter_id\": \"8c7fcd35-5fda-4eff-b368-f373fefc8327\", \"data_category\": \"Simple Nucleotide Variation\", \"acl\": [\"phs000748\"], \"type\": \"simple_somatic_mutation\", \"file_size\": 29042, \"state_comment\": null, \"created_datetime\": \"2019-01-28T10:21:43.275213-06:00\", \"md5sum\": \"0138a8506fdbca4cf5b6c9e71c61f0b7\", \"updated_datetime\": \"2019-06-24T08:07:19.797044-05:00\", \"file_id\": \"1294fc89-fae1-4d71-b970-dee321eeb6c1\", \"error_type\": null, \"data_type\": \"Raw Simple Somatic Mutation\", \"state\": \"released\", \"experimental_strategy\": \"WXS\", \"version\": \"1\", \"data_release\": \"18.0 - 34.0\"}], \"pagination\": {\"count\": 1, \"total\": 863977, \"size\": 1, \"from\": 0, \"sort\": \"\", \"page\": 1, \"pages\": 863977}}, \"warnings\": {}}"

Created on 2022-08-02 by the reprex package (v2.0.1)

Note that the curl system command returns JSON format by default.

LiNk-NY commented 2 years ago

It looks like I needed to use accept_json():

response <- POST("https://api.gdc.cancer.gov/files?",
    query = list(
        size=1
    ), encode = "json", accept_json(), content_type_json()
)