简体   繁体   English

Curl有效,但Python请求无效

[英]Curl Works, but Python Requests Doesn't

I'm trying to build a bot that will automatically schedule meeting rooms at my school. 我正在尝试构建一个自动在我学校安排会议室的机器人。 Currently there is a website that handle this, but it is very tedious. 当前有一个处理此问题的网站,但这非常繁琐。 I was able to get the smallest CURL command that can successfully make these reservations. 我能够获得可以成功进行这些保留的最小的CURL命令。 However when I switch to using python requests it gets an error like I booked too many sites. 但是,当我切换为使用python请求时,它会收到错误消息,例如我预订了太多站点。 However if I run the identical curl command after it there is no error. 但是,如果在它之后运行相同的curl命令,则不会出现错误。 I think something is wrong with the HTTP request but I have no idea where to even begin with that. 我认为HTTP请求出了点问题,但我不知道从哪里开始。

Curl: 卷曲:

    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 Requests: 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)

Curl Output: 卷曲输出:

{"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 Requests Output: 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': ''}

The curl will return the same as the original. 卷曲将恢复为与原始相同。 So I only hit my quota when I use requests making me think it is some other error. 因此,只有在使用请求时才达到配额,这使我认为这是其他错误。

Any idea why one seems to return a website(status=200) with an error vs making the reservation? 知道为什么有人似乎会错误地返回网站(status = 200)与进行预订吗?

PS: I've seen this question asked a few times but most seem to be an auth issue. PS:我看过几次这个问题,但大多数似乎是auth问题。 Mine feels totally different. 我的感觉完全不同。

I tried with curl and requests, got same error message ({"status":0,"msg":"Error - No user data submitted","type":""}, here is my code in order to use requests: 我尝试了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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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