GET request works in Chrome but not with httr

If I copy and paste the urls below into Chrome I get a json response, but using the httr::GET function results in a 502 from the server. Can anyone explain to me what is going on?

Update:a confirmation that you get a response in the browser and not with the httr GET request will also be helpful :slight_smile:


library(httr)

url1 <- 'http://data.stats.gov.cn/english/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=reg&colcode=sj&wds=[{"wdcode":"zb","valuecode":"A090201"}]&dfwds=[{"wdcode":"sj","valuecode":"1995-2014"}]&k1=1472740901192'
url2 <- 'http://data.stats.gov.cn/english/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=reg&colcode=sj&wds=[{%22wdcode%22:%22zb%22,%22valuecode%22:%22A090201%22}]&dfwds=[{%22wdcode%22:%22sj%22,%22valuecode%22:%221995-2014%22}]&k1=1472740901192'

res <- httr::GET(url1)
res
#> Response [http://data.stats.gov.cn/english/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=reg&colcode=sj&wds=[{"wdcode":"zb","valuecode":"A090201"}]&dfwds=[{"wdcode":"sj","valuecode":"1995-2014"}]&k1=1472740901192]
#>   Date: 2019-06-30 11:02
#>   Status: 502
#>   Content-Type: <unknown>
#>   Size: 281 B
#> <BINARY BODY>
res <- httr::GET(url1, httr::add_headers("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"))
res
#> Response [http://data.stats.gov.cn/english/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=reg&colcode=sj&wds=[{"wdcode":"zb","valuecode":"A090201"}]&dfwds=[{"wdcode":"sj","valuecode":"1995-2014"}]&k1=1472740901192]
#>   Date: 2019-06-30 11:02
#>   Status: 502
#>   Content-Type: <unknown>
#>   Size: 281 B
#> <BINARY BODY>

res <- httr::GET(url2)
res
#> Response [http://data.stats.gov.cn/english/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=reg&colcode=sj&wds=[{%22wdcode%22:%22zb%22,%22valuecode%22:%22A090201%22}]&dfwds=[{%22wdcode%22:%22sj%22,%22valuecode%22:%221995-2014%22}]&k1=1472740901192]
#>   Date: 2019-06-30 11:02
#>   Status: 502
#>   Content-Type: <unknown>
#>   Size: 281 B
#> <BINARY BODY>
res <- httr::GET(url2, httr::add_headers("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"))
res
#> Response [http://data.stats.gov.cn/english/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=reg&colcode=sj&wds=[{%22wdcode%22:%22zb%22,%22valuecode%22:%22A090201%22}]&dfwds=[{%22wdcode%22:%22sj%22,%22valuecode%22:%221995-2014%22}]&k1=1472740901192]
#>   Date: 2019-06-30 11:02
#>   Status: 502
#>   Content-Type: <unknown>
#>   Size: 281 B
#> <BINARY BODY>

Yes, I get the same with httr.

I manage to read the urls with jsonlite

library(httr)

url1 <- 'http://data.stats.gov.cn/english/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=reg&colcode=sj&wds=[{"wdcode":"zb","valuecode":"A090201"}]&dfwds=[{"wdcode":"sj","valuecode":"1995-2014"}]&k1=1472740901192'
url2 <- 'http://data.stats.gov.cn/english/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=reg&colcode=sj&wds=[{%22wdcode%22:%22zb%22,%22valuecode%22:%22A090201%22}]&dfwds=[{%22wdcode%22:%22sj%22,%22valuecode%22:%221995-2014%22}]&k1=1472740901192'

res <- jsonlite::fromJSON(url1)
str(res, 2)
#> List of 2
#>  $ returncode: int 200
#>  $ returndata:List of 2
#>   ..$ datanodes:'data.frame':    620 obs. of  3 variables:
#>   ..$ wdnodes  :'data.frame':    3 obs. of  3 variables:
res2 <- jsonlite::fromJSON(url2)
str(res2, 2)
#> List of 2
#>  $ returncode: int 200
#>  $ returndata:List of 2
#>   ..$ datanodes:'data.frame':    620 obs. of  3 variables:
#>   ..$ wdnodes  :'data.frame':    3 obs. of  3 variables:

Created on 2019-06-30 by the reprex package (v0.3.0)

1 Like

There might be some location restriction based on IP because I still get an error. Thanks for the effort! :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.