Curl有效,但Python请求无效

时间:2019-02-13 20:01:32

标签: python http curl python-requests

我正在尝试构建一个自动在我学校安排会议室的机器人。当前有一个处理此问题的网站,但这非常繁琐。我能够获得可以成功进行这些保留的最小的CURL命令。但是,当我切换为使用python请求时,它会收到错误消息,例如我预订了太多站点。但是,如果在它之后运行相同的curl命令,则不会出现错误。我认为HTTP请求出了点问题,但是我不知道从哪里开始。

卷曲:

    curl 'https://api3.libcal.com/process_roombookings.php?m=booking_mob&iid=1723' \
-XPOST \
-H 'Referer: https://api3.libcal.com/room_widget.php?gid=4066&iid=1723' \
--data 'gid=4066&sid%5B%5D=660065516&fname=<First Name>&lname=<Last Name>&email=<School Email>&q1=<Student ID>&q2=4-6&q3=Engineering&q4=CPE&qcount=4&fid=2166'

Python请求:

url = 'https://api3.libcal.com/process_roombookings.php'
    headers = {
        'Referer': 'https://api3.libcal.com/room_widget.php?gid=4066&iid=1723',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'X-Requested-With': 'XMLHttpRequest'
    }
    params = (
        ('m', 'booking_mob'),
        ('iid', '1723'),
    )
    data = {
        'gid': '4066',
        'sid': ['660065216'],
        'fname': '<First Name>',
        'lname': '<Last Name>',
        'email': '<Personal email>',
        'q1': '<Student Id>',
        'q2': '4-6',
        'q3': 'Engineering',
        'q4': 'CPE',
        'qcount': '4',
        'fid': '2166'
    }

    request = requests.request('POST', url, headers=headers, params=params, data=data)

卷曲输出:

{"status":2,"msg":"<div class=\"alert alert-warning\"><p><strong>This booking is tentative only!<\/strong><\/p><p><span class='rm_book_tent'>Room I, 12:00am - 1:00am Friday, February 15, 2019 - Tentative<\/span><br\/><\/p><p>You must confirm the booking within 1 hour,  via the URL contained in the email just sent to you.<\/p><\/div>","type":""}

Python请求输出:

{'status': 2, 'msg': '<div class="alert alert-danger"><p><strong>You have exceeded the booking limits/quota:</strong></p><p></p><p></p></div>', 'type': ''}

卷曲将恢复为与原始相同。因此,只有在使用请求时才达到配额,让我觉得这是其他错误。

有人知道为什么似乎要进行预订而返回错误的网站(status = 200)吗?

PS:我看过几次这个问题,但大多数似乎是auth问题。我的感觉完全不同。

1 个答案:

答案 0 :(得分:0)

我尝试了curl和请求,收到了相同的错误消息({“ status”:0,“ msg”:“错误-未提交用户数据”,“ type”:“”},这是我的代码,使用请求:

import requests

headers = {
    'Referer': 'https://api3.libcal.com/room_widget.php?gid=4066&iid=1723',
}

params = (
    ('m', 'booking_mob'),
    ('iid', '1723'),
)

data = {
  'gid': '4066',
  'sid[]': '660065516',
  'fname': '<First Name>',
  'lname': '<Last Name>',
  'email': '<School Email>',
  'q1': '<Student ID>',
  'q2': '4-6',
  'q3': 'Engineering',
  'q4': 'CPE',
  'qcount': '4',
  'fid': '2166'
}

response = requests.post('https://api3.libcal.com/process_roombookings.php', headers=headers, params=params, data=data)
相关问题