Unable to resolve 503 error with httr POST request

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)

I think you should use list instead of c to create the body

library(httr)
body <- list('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)

status_code(r)
#> [1] 200

Created on 2019-04-02 by the reprex package (v0.2.1.9000)

the body must be form encoded like in your python request and so needs to be as list. the default value in POST is encode = "form".

2 Likes

Thank you so much for that. Really appreciate it.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.