简体   繁体   中英

Can not receive json-response after post-request Python

My task is to get a calendar of booking of apartments from the site pages https://www.dobovo.com/ . The site itself receives data from the post-request in json format when the page is loaded. When I send a similar request, I get the following error {'success': False, 'error': 'Method GET is not allowed'} , although I post the request by the post method.

Interestingly, a few days ago I could get the data in the same way. It's also funny that I can get this json from the terminal: curl -d "id=89740&date=2017-07-01" https://www.dobovo.com/dobovo/apt/ajax.php?action=getCalendar&lang=en or wget --post-data="id=82731&date=2017-07-01" https://www.dobovo.com/dobovo/apt/ajax.php?action=getCalendar&lang=en (id = 82731 is an example for one apartment)

My Python code:

from requests import post
def get_calendar(url):
    ajax = 'http://www.dobovo.com/dobovo/apt/ajax.php?action=getCalendar&lang=en'
    first_day_in_month = '2017-07-01'
    id_ = url[-10:-5]
    form_data = {
        'id': id_,
        'date': first_day_in_month
    }
    response = post(url=ajax, data=form_data)
    return response.json()

url here is a link to a page with a calendar, like https://www.dobovo.com/zaporozhye-apartments/orchid-82731.html

Tell me, please, what am I doing wrong?

According to sigmavirus24's answer , POST request is changed to GET only when redirect happens. Seems like dobovo.com works in https only, so change 'http' to 'https' in your ajax variable and you will be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM