javaapihttp-status-code-403

Verbix: 403 error in Java API but not browser or Postman


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


Solution

  • 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.