0

I need to get the content of a file, which I have found with chromes inspector under the network tab. To achieve this, I want to send a post request with the required payload to the request URL. Since I have used the Selenium ChromeDriver to navigate and log into the page, I first tried using the HtmlRequest class provided by Selenium. But I was stuck after just a few seconds, when I did not know how to pass in the payload or how to execute the request once I would be eventually done. Since I was unable to find a proper explanation or example of this class, I did not get any further than the following code:

HttpRequest xhr = new HttpRequest(HttpMethod.POST, "https://flow.polar.com/api/training/history");
xhr.setContent(?);

Next, I tried using the HttpClient class from java.net.http. I found the following example and changed the URL to the one, the chrome inspector presented me as the request URL.

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://flow.polar.com/api/training/history")).build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();

This returned a html site, which just tells me, that the site with the URL I put in could not be found.

I then tried using the JavascriptExeucutor of Selenium with the following code:

JavascriptExecutor js = (JavascriptExecutor) driver;
System.out.println(js.executeScript("var xhr = new XMLHttpRequest();\r\n" + 
        "xhr.open('POST', 'https://flow.polar.com/api/training/history', false);\r\n" + 
        "xhr.setRequestHeader('XHR', 'application/x-www-form-urlencoded');\r\n" + 
        "\r\n" + 
        "xhr.send('login=test&password=test');\r\n" + 
        "return xhr.response;", (Object) null));

This returns html code, which shows the following website: The website

It would be really nice to get an answer, because I have spent way too much time trying to figure out a way to do this (multiple attempts with iterations of the code examples above were conducted, however they yielded the same or worse results).

Here are screenshots of the chrome inspector on the wanted file: chromeInspector1

chromeInspector2

mousekip
  • 117
  • 10
  • So it'd be: 1. Login to get access_token. 2 Make POST request with body (as in image) and access_token(maybe in header), save response to file. This flow can be quite easy when using HTTP lib in java, Rest-assured is a good choice. – lucas-nguyen-17 Oct 11 '21 at 12:54
  • @lucasnguyen17 Well I certainly do not know how to log into a website and make a post request with java.net.http, so could you maybe provide an example or a tutorial? – mousekip Oct 11 '21 at 13:31

1 Answers1

0

The error say all about that. 403 means you are not authorized to POST. That means, You are trying to create a resource without Authorization.

enter image description here

You need to generate authorization token and use that for your POST request.

Please refer Http Basic Authentication in Java using HttpClient?

Ravik
  • 694
  • 7
  • 12
Nandan A
  • 2,702
  • 1
  • 12
  • 23
  • I tried the code from the question you provided, but it still throws the following IOException in the console:Server returned HTTP response code: 403 for URL: https://flow.polar.com/api/training/history. Oh and if I try the same code with google, it throws: Server returned HTTP response code: 411 for URL: https://www.google.de/?hl=de – mousekip Oct 11 '21 at 13:52
  • Have you generated valid authorization ket? – Nandan A Oct 11 '21 at 13:58
  • I do not exactly know. I just went to the thread you provided, copied the accepted answer, changed the url and ran it. – mousekip Oct 11 '21 at 14:02
  • That is for reference I provided. You need to generate a authorization token using your credentials then include that token in your `POST` request. – Nandan A Oct 11 '21 at 14:03
  • Well I have no idea how to generate an authorization token or how to include it in my request. – mousekip Oct 11 '21 at 14:06