1

This API request works from Postman, but not Java. How can I get it working? Its returning this Java 403 error.

Java Error: Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.verbix.com/webverbix/korean/%EB%B0%9B%EB%8B%A4

Looking at previous stack articles, but no luck yet:

Web Client PUT request returns 403 but Postman request does not - Jersey REST API on Glassfish

Postman:

enter image description here

Java:

public static void main(String[] args) throws IOException {
    URL url = new URL("https://api.verbix.com/conjugator/iv1/6153a464-b4f0-11ed-9ece-ee3761609078/1/8442/8442/%EB%B0%9B%EB%8B%A4");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer content = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        content.append(inputLine);
    }
    System.out.println("JSON String Result " + content.toString());
    in.close();
}

Resources:

Unable to retrieve table elements using jsoup

mattsmith5
  • 540
  • 4
  • 29
  • 67
  • hi @OldProgrammer mistypo, I just fixed, same result as testing https://api.verbix.com/conjugator/iv1/6153a464-b4f0-11ed-9ece-ee3761609078/1/8442/8442/%EB%B0%9B%EB%8B%A4 – mattsmith5 Apr 15 '23 at 00:09
  • You need to use HttpsURLConnection, not HttpURLConnection – OldProgrammer Apr 15 '23 at 00:14
  • like this ? @OldProgrammer HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); still getting same error, feel free to post an answer, test, and I can send points, thanks ! no luck yet – mattsmith5 Apr 15 '23 at 00:15

1 Answers1

1

You are missing some request headers. Add the setRequestProperty calls below

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class Test {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://api.verbix.com/conjugator/iv1/6153a464-b4f0-11ed-9ece-ee3761609078/1/8442/8442/%EB%B0%9B%EB%8B%A4");
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");

...
}

If you look under the Headers tab in Postman for the request, you should see something similar.

OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
  • thanks added those in, was using simple example here, will add more https://www.baeldung.com/java-http-request – mattsmith5 Apr 15 '23 at 00:40