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

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

potterzot opened this issue Dec 20, 2017 · 3 comments
Labels
bug

Comments

@potterzot
Copy link

@potterzot potterzot commented Dec 20, 2017

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 hidden.

@hadley

This comment has been hidden.

@hadley hadley added the reprex label Nov 21, 2018
@hadley
Copy link
Member

@hadley 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 and removed reprex labels Nov 22, 2018
@hadley hadley closed this in 8ff486a Nov 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants