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

Comments

Projects
None yet
3 participants
@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 was marked as outdated.

MikeBadescu commented Sep 12, 2018

I confirm this issue.

@hadley

This comment was marked as resolved.

Member

hadley commented Nov 21, 2018

Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session.

@hadley hadley added the reprex label Nov 21, 2018

@hadley

This comment has been minimized.

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