Search code examples
javajsoup

Query train arrivals/departures from https://enquiry.indianrail.gov.in/mntes


i am trying to get list of stops(stations) for a train by passing train number and other required parameters(got from web developer tools-firefox) with the url(POST method), but i get 404-page not found error code. when i tried with POSTMAN, it gets the webpage with the requested data, what is wrong with the code?

        Document doc= Jsoup.connect("https://enquiry.indianrail.gov.in/mntes/q?")
                .data("opt","TrainRunning")
                .data("subOpt","FindStationList")
                .data("trainNo",trainNumber)
                .data("jStation","")
                .data("jDate","25-Aug-2021")
                .data("jDateDay","Wed")
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0")
                .referrer("https://enquiry.indianrail.gov.in/mntes/")
                .ignoreHttpErrors(true)
                .post();
        System.out.println(doc.text());

Screen Shot of POSTMAN

thank you in advance


Solution

  • I've tried to make the request work with Jsoup but to no avail. An odd way of sending form data is being used. Form data is passed as URL query parameters in a POST request.

    Jsoup uses a simplified HTTP API in which this particular use case was not foreseen. It is debatable whether it is appropriate to send form parameters the way https://enquiry.indianrail.gov.in/mntes expects them to be sent.

    If you're using Java 11 or later, you could simply fetch the response of your POST request via the modern Java HTTP Client. It fully supports the HTTP protocol. You can then feed the returned String into Jsoup.

    Here's what you could do:

    // 1. Get the response
    HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://enquiry.indianrail.gov.in/mntes/q?opt=TrainRunning&subOpt=ShowRunC&trainNo=07482&jDate=25-Aug-2021&jDateDay=Wed&jStation=DVD%23false"))
        .POST(BodyPublishers.noBody())
        .build();
    HttpResponse<String> response =
        client.send(request, BodyHandlers.ofString());
    
    // 2. Parse the response via Jsoup
    Document doc = Jsoup.parse(response.body());
    System.out.println(doc.text());
    

    I've simply copy-pasted the proper URL from Postman. You might want to build your query string in a more robust way. See: