0

i am scraping a website. I want to send a post request with form data to a url.

I have been able to achieve that in python but in javascript i am not receiving the proper response

# python
import requests

response = requests.post(
    url = 'https://savings.gov.pk/latest/results.php',
    data= {
        'country'       : '1',
        'state'         : 'all',
        'range_from'    : '000000',
        'range_to'      : '000100',
        'pb_number_list': '',
        'btnsearch'     : 'Search'
    }
)
print(response.text.find('class="divTable"'))

>>> 11699
// javascript
import axios from 'axios'

let response = await axios.post(
    'https://savings.gov.pk/latest/results.php', 
    {
        country       : '1',
        state         : 'all',
        range_from    : '000000',
        range_to      : '000100',
        pb_number_list: '',
        btnsearch     : 'Search'
    }
)
console.log(response.data.search('class="divTable"'))

>>> -1

I am using axios library for javascript, I believe I am not receiving the correct response because I am not passing the data correctly.

any idea how should I be passing the data? if this is achieved using a different library than that is also acceptable.

hammi
  • 804
  • 5
  • 14
  • What type of data does the POST consume on the server? Form data or JSON? You may need to construct a `FormData` object from the object (JSON-like structure) you are sending in JS. For example, see [_Convert JS Object to form data_](https://stackoverflow.com/a/64527862/1762224) ~ _"Since version 0.27 Axios supports automatic object serialization to a FormData object if the request Content-Type header is set to multipart/form-data."_ – Mr. Polywhirl Apr 05 '23 at 12:55

0 Answers0