Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If a response header has "content-type" = "", content() gives an error #500

Closed
potterzot opened this issue Dec 20, 2017 · 3 comments
Closed
Labels
bug an unexpected problem or unintended behavior

Comments

@potterzot
Copy link

Reproducible example:

library(httr)
#fabricated response from USDA NASS QuickStats API
r <- list(
  url = "https://quickstats.nass.usda.gov/api/api_GET?key=<key removed>&sector_desc=ECONOMICS&commodity_desc=RENT&prodn_practice_desc=IRRIGATED&class_desc=CROPLAND&agg_level_desc=COUNTY&year__GE=1997&format=JSON",
  date = as.POSIXct("2017-12-20 22:43:47 GMT"),
  headers = list('content-type' = ""),
  content = charToRaw('{\"error\":[\"bad request - invalid query\"]}'))

class(r) <- "response"
class(r$headers) <- "insensitive"

#Call to httr::content without specifying type
content(r, as="text")

> Error in if (length(m) == 1 && m == -1) { : 
  missing value where TRUE/FALSE needed

#Works if specify type
content(r, as="text", type="text/json")
> '{\"error\":[\"bad request - invalid query\"]}'

This seems to be in the parse_media() function in media-parse.r, which works fine if type is NULL but not if type is "". type gets set to "" in the content() function if it is "" in the header.

@MikeBadescu

This comment has been minimized.

@hadley

This comment has been minimized.

@hadley hadley added the reprex needs a minimal reproducible example label Nov 21, 2018
@hadley
Copy link
Member

hadley commented Nov 22, 2018

library(httr)

r <- list(
  url = "https://quickstats.nass.usda.gov/api/api_GET?key=<key removed>&sector_desc=ECONOMICS&commodity_desc=RENT&prodn_practice_desc=IRRIGATED&class_desc=CROPLAND&agg_level_desc=COUNTY&year__GE=1997&format=JSON",
  date = as.POSIXct("2017-12-20 22:43:47 GMT"),
  headers = list('content-type' = ""),
  content = charToRaw('{\"error\":[\"bad request - invalid query\"]}')
)

class(r) <- "response"
class(r$headers) <- "insensitive"

content(r, as="text")
#> Error in if (length(m) == 1 && m == -1) {: missing value where TRUE/FALSE needed

Created on 2018-11-22 by the reprex package (v0.2.1)

@hadley hadley added bug an unexpected problem or unintended behavior and removed reprex needs a minimal reproducible example labels Nov 22, 2018
@hadley hadley closed this as completed in 8ff486a Nov 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants