Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The correct POST request
#1
Hi everyone. I have been trying to scrape a website to get the data of the next day. https://www.powerexindia.com/code/fronte...rice.html/. I am sending the correct data in POST request but still i am getting the webpage of current date. There is only error if I do not send the csrftoken otherwise no matter what date i send i am getting the webpage of current date data. Please help in knowing where i am wrong.
        with requests.Session() as session:
            try:
                res = session.get(self.BASE_URL, headers=headers)
            except requests.exceptions.Timeout as e:
                print(standard_datetime(), FILENAME, date, e)
            else:
                csrf = res.cookies.get("csrftoken")
                if csrf is None:
                    return None
            headers.update({"X-CSRFToken": csrf})
            cookies = {"csrftoken": csrf}
            payload = {
                "dataview12": "Daily",
                "fromhour": "0",
                "tohour": "95",
                "DeliveryDate": "2020-06-07",
                "csrfmiddlewaretoken": csrf,
                "markall": "%",
                "N1": "N1", "N2": "N2", "N3": "N3",
                "E1": "E1", "E2": "E2",
                "W1": "W1", "W2": "W2", "W3": "W3",
                "S1": "S1", "S2": "S2", "S3": "S3",
                "A1": "A1", "A2": "A2",
                "DeliveryfromDate": "", "DeliverytoDate": "",
                "year": "2020", "month": "1",
                "submit": "submit"
            }
            print(payload)
            try:
                res = session.post(
                    self.BASE_URL, headers=headers, cookies=cookies
                )
            except requests.exceptions.Timeout as e:
                print(standard_datetime(), FILENAME, date, e)
            except requests.exceptions.SSLError as e:
                print(standard_datetime(), FILENAME, date, e)
            except requests.exceptions.ConnectionError as e:
                print(standard_datetime(), FILENAME, date, e)
            else:
                return res
Reply
#2
(Jun-04-2020, 05:11 PM)abhie_lp Wrote: I am sending the correct data in POST request
you never post the payload (assuming it's a correct one)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
This won't answer the original question -- it's just FYI. I'm no expert, but I have come across commercial web services that require a POST request with parameters in order to retrieve the data. I don't think you can send a JSON payload with a GET request, right? Again, I only ran into this once with a service we had to call. Most other times, I use query strings. <Disclaimer: I'm retired and old and may not know what I'm talking about>.
Reply
#4
Their POST request is on line 30. The get request is used to get CSRF token
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Jun-05-2020, 02:16 AM)buran Wrote: Their POST request is on line 30. The get request is used to get CSRF token

I updated it. Actually i was trying whether i will get any error if i send the POST request without data. Thanks.
Reply
#6
So do you still have a problem? Post your actual code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Python request (post/get) Drunknmonkie 1 2,668 Jan-19-2023, 02:02 PM
Last Post: prvncpa
  Post request not saving in DRF Dexty 0 927 Jun-03-2022, 12:35 PM
Last Post: Dexty
Brick how to work with 2 or more related tables in a Post request ikurorox 0 1,406 Dec-05-2021, 01:01 AM
Last Post: ikurorox
  POST request with form data issue web scraping hoff1022 1 2,694 Aug-14-2020, 10:25 AM
Last Post: kashcode
  How to apply post request in python for establishment search for EPFO? rajeev1729 0 1,888 May-01-2020, 04:45 PM
Last Post: rajeev1729
  Scraping problems. Pls help with a correct request query. gtlhbkkj 0 1,511 Oct-09-2019, 12:00 PM
Last Post: gtlhbkkj
  Scraping problems. Pls help with a correct request query. gtlhbkkj 6 3,090 Oct-01-2019, 09:22 PM
Last Post: gtlhbkkj
  Handle parameters in POST request for python webserver? JBristow1729 1 7,771 Jul-16-2019, 10:59 PM
Last Post: scidam
  submit element from a list into a post request Godzilla 0 3,956 Mar-24-2018, 02:35 PM
Last Post: Godzilla
  I need request POST for Facebook in My Profile Kalet 4 4,322 Sep-27-2017, 05:53 PM
Last Post: Kalet

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020