0

POSTMAN Screenshot

I'm trying to send a POST request to grab comments but it doesn't work in Java while it does work with postman.

I get an 403 Forbidden error, but on postman it retrieves the data i need just fine..

Here's the Java code I'm trying to use to replicate the behavior.

String targetUrl = YOUTBE_COMMENTS_AJAX_URL;
    String urlParameters = "action_load_comments=1&order_by_time=True&filter=jBjXVrS8nXs";
    String updatedURL = targetUrl + "?" + urlParameters;
    URL url = null;
    InputStream stream = null;
    HttpURLConnection urlConnection = null;
    try {
        url = new URL(updatedURL);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("content-type", "multipart/form-data");
        urlConnection.setRequestProperty("user-agent", "USER_AGENT");
        urlConnection.setDoOutput(true);

        String data = URLEncoder.encode("video_id", "UTF-8")
                + "=" + URLEncoder.encode(youtubeId, "UTF-8");

        data += "&" + URLEncoder.encode("session_token", "UTF-8") + "="
                + URLEncoder.encode(xsrfToken, "UTF-8");

        data += "&" + URLEncoder.encode("page_token", "UTF-8") + "="
                + URLEncoder.encode(pageToken, "UTF-8");
        urlConnection.connect();

        OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
        wr.write(data);
        wr.flush();

        stream = urlConnection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"), 8);
        String result = reader.readLine();
        return result;

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return null;

Here's an example of what postman is sending in their headers

  • Looks like you're not sending the headers as your postman request does. – Luiggi Mendoza Jul 13 '18 at 20:40
  • Possible duplicate of [How can I POST using Java and include parameters and a raw request body?](https://stackoverflow.com/questions/12666702/how-can-i-post-using-java-and-include-parameters-and-a-raw-request-body) – VeeArr Jul 13 '18 at 20:43
  • But aren't I setting the headers when i do setRequestProperty? Those are the only 2 I set in postman – Elvin Uthuppan Jul 13 '18 at 20:50

1 Answers1

0

It seems like your problem is here (see inline comments):

DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(urlParameters); 
// you wrote your URL parameters into Body
wr.flush();
wr.close(); 
//You closed your body and told server - you are done with request
conn.getOutputStream().write(postDataBytes); 
// you wrote data into closed stream - server does not care about it anymore. 
  • You have to append your urlParameters directly to the URL when you open it
  • Then you have to write your Form Data into body as you do: conn.getOutputStream().write(postDataBytes);
  • and then close output stream
Vadim
  • 4,027
  • 2
  • 10
  • 26
  • Alright so I did exactly what you said as shown in the edited code, but now I get an error 403 "Forbidden" instead of 400. So I guess its atleast being sent, but I suppose not encoded properly? Idk if this helps, but when I try to send it raw through postman I get the same thing, but sending it as key-value pairs through postman's form-data or x-www-form-urlencoded works. I figured it should work using Java's urlencode method , but getting this error is putting me at a loss :/ – Elvin Uthuppan Jul 13 '18 at 21:34
  • Not necessary encoding is a problem. 403 is about authorization. It is up to the server. I can suspect value of `session_token` or `page_token`. It also can be value of User-Agent. With your last update: are you sure that video_id and session_token both are the same? good luck... – Vadim Jul 13 '18 at 22:03
  • whoops... good catch on the value, but I still do get the same issue. Weirdly enough, it goes through postman with the same user agent and i printed to make sure the session token comes out properly. It's the same thing I feed postman. I updated the post yet again with a pic of what postman is sending exactly. – Elvin Uthuppan Jul 13 '18 at 22:15