I am translating a working python web-scraping script to R and running from RStudio Version 1.1.456.
I am getting a Status: 503 return code. This is frequently a result of not passing the right headers to the server, particularly a missing User-Agent, however, I am passing the same headers in R as in my working Python script.
I have looked through lots of questions online and the httr documentation governing POST requests and still can't see what I am doing wrong. I know that R doesn't have a dictionary object, so I am using a vector to pass the body of the POST request. I am wondering if the problem resides there?
The expected result would be a status 200 at this point (I haven't gone on to add the rest of the Python script translation given the 503 fail).
Please can I have suggestions on how to resolve this 503 error?
R script:
library(httr)
body <- c('search[filter_enum_make]' = 'alfa-romeo',
'search[dist]' = '5',
'search[category_id]' = '29')
r <- POST("https://www.otomoto.pl/ajax/search/list/",
add_headers(headers = c('User-Agent' = 'Mozilla/5.0')),
body = body)
print(r)
Python Script (working - reduced code to match R script objective):
import requests
headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
}
data = {
'search[filter_enum_make]': 'alfa-romeo',
'search[dist]' : '5',
'search[category_id]' : '29'
}
r = requests.post('https://www.otomoto.pl/ajax/search/list/', data = data, headers = headers)
print(r.status_code)