rest api post submissions(Japp)

Jesús Alberto Domínguez Alfonso jesusalbertodominguez1983 at gmail.com
Tue Aug 9 21:16:41 CEST 2016


Hello ,

Thanks in advance.

Please i need help.

About your answwer:(Japp)
"The REST API is an "application programming interface". The web method
does not use it, but "talks" to the webserver directly. Also you don't
need libcurl, but this is just one (easy) way of writing code to access
the REST API"

My problem is a bad request 400, I understand that the problem is that i
send the parameters not correctly format and for this problem submmit post
rest api doesn,t work.

Please if someone know the correctly form to send the parameters, please
wrote to me or send me an code example.

Now i am going to put two methods make for a friend and me, this methods
don´t work, we recieve a bad request 400, he and i are thinking the
problems are some parameters that post doesn,t undesrtand,(
http://localhost/domjudge/api/submissions           POST)



methods
------------------------------------------------------------
private void testConsumerPOST(String array, String nombreProblema, String
languageId, String contest_id) {
        String servletURL = "http://localhost/domjudge/api/submissions";

        URL servlet = null;
        try {

            TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    public java.security.cert.X509Certificate[]
getAcceptedIssuers() {
                        return null;
                    }

                    public void checkClientTrusted(X509Certificate[] certs,
String authType) {
                    }

                    public void checkServerTrusted(X509Certificate[] certs,
String authType) {
                    }

                }
            };

            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.
getSocketFactory());

            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };
            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

            servlet = new URL(servletURL);
            *HttpURLConnection servletConnection = (HttpURLConnection)
servlet.openConnection();*

            // AUTENTICACION










*String userpass = "admin:admin";             String basicAuth = "Basic " +
new String(new
com.sun.org.apache.xerces.internal.impl.dv.util.Base64().encode(userpass.getBytes()));
            servletConnection.setRequestProperty("Authorization",
basicAuth);             servletConnection.setRequestMethod("POST");
            servletConnection.setDoOutput(true);
servletConnection.setDoInput(true);             OutputStreamWriter out =
new OutputStreamWriter(servletConnection.getOutputStream());
ObjectMapper mapper = new ObjectMapper();             Map<String, Object>
data = new HashMap<String, Object>();*




*data.put("code[]", array);             data.put("shortname",
nombreProblema);             data.put("langid", languageId);
data.put("contest", contest_id);*


*out.write(mapper.writeValueAsString(data));             *out.flush();

            if (*servletConnection.getResponseCode() !=
HttpURLConnection.HTTP_CREATED*) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + servletConnection.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (servletConnection.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

            out.close();

        } catch (MalformedURLException ex) {
            Logger.getLogger(PostSubmission.class.getName()).log(Level.SEVERE,
null, ex);
        } catch (ProtocolException ex) {
            Logger.getLogger(PostSubmission.class.getName()).log(Level.SEVERE,
null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PostSubmission.class.getName()).log(Level.SEVERE,
null, ex);
        } catch (KeyManagementException ex) {
            Logger.getLogger(PostSubmission.class.getName()).log(Level.SEVERE,
null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(PostSubmission.class.getName()).log(Level.SEVERE,
null, ex);
        }
    }




Also another one with binary data post almost identical:

private void testConsumerPOST_Binary(String array, String nombreProblema,
String languageId, String contest_id) {
        String servletURL = "http://192.168.1.146/domjudge/api/submissions";

        try {

            TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    public java.security.cert.X509Certificate[]
getAcceptedIssuers() {
                        return null;
                    }

                    public void checkClientTrusted(X509Certificate[] certs,
String authType) {
                    }

                    public void checkServerTrusted(X509Certificate[] certs,
String authType) {
                    }

                }
            };

            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.
getSocketFactory());

            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };
            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);























*HttpURLConnection httpcon = (HttpURLConnection) ((new
URL(servletURL).openConnection()));             httpcon.setDoOutput(true);
            httpcon.setRequestProperty("Content-Type", "application/json");
            httpcon.setRequestProperty("Accept", "application/json");
            httpcon.setRequestMethod("POST");             String userpass =
"admin:admin";             String basicAuth = "Basic " + new String(new
com.sun.org.apache.xerces.internal.impl.dv.util.Base64().encode(userpass.getBytes()));
            httpcon.setRequestProperty("Authorization", basicAuth);
            httpcon.connect();             ObjectMapper mapper = new
ObjectMapper(); //Jackson Object mapper             Map<String, Object>
data = new HashMap<String, Object>();             data.put("code[]",
array);             data.put("shortname", nombreProblema);
data.put("langid", languageId);             data.put("contest",
contest_id);             byte[] outputBytes =
mapper.writeValueAsString(data).getBytes("UTF-8");             OutputStream
os = httpcon.getOutputStream();             os.write(outputBytes);*
            os.flush();

            if (*httpcon.getResponseCode() !=
HttpURLConnection.HTTP_CREATED*) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + httpcon.getResponseCode());
            }

            //Get the new submission_id
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (httpcon.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

            os.close();

        } catch (MalformedURLException ex) {
            Logger.getLogger(PostSubmission.class.getName()).log(Level.SEVERE,
null, ex);
        } catch (ProtocolException ex) {
            Logger.getLogger(PostSubmission.class.getName()).log(Level.SEVERE,
null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PostSubmission.class.getName()).log(Level.SEVERE,
null, ex);
        } catch (KeyManagementException ex) {
            Logger.getLogger(PostSubmission.class.getName()).log(Level.SEVERE,
null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(PostSubmission.class.getName()).log(Level.SEVERE,
null, ex);
        }
    }

None of them are able to submit the files, just to be clear about the data
format Im sending:

*code[]* ---> "[{\"filename\":\"main.cpp\",\"content\":\"
I2luY2x1ZGUgPGlvc3RyZWFtPg0KI2luY2x1ZGUgPHN0ZGlvLmg+
DQoNCnVzaW5nIG5hbWVzcGFjZSBzdGQ7DQoNCmludCBtYWluKCkNCnsNCiAg
ICBTdHJpbmcgbm9tYnJlPSJwZWRybyI7DQoJaW50IHZhbD0xLHZhMj0xMCx0
b3RhbD0wOw0KCXRvdGFsPXZhbCt2YWwyOw0KCQ0KCQ0KICAgIHJldHVybiAwOw0KfQ0K\"}]"

*shortname* ---> The problem ddbb shortname value previously recovered with
the "problems GET method of the API REST"

*langid*---> The language id value previously recovered with the "problems
GET method of the API REST" (Ex: cpp)

*contest* ---> We have tried both contestId and contest_short_name. Im not
sure wich one should be sent.

My last question is: Is the teamId necessary for the post?
-------------------------------------------------------------



thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.domjudge.org/pipermail/domjudge-devel/attachments/20160809/5a460679/attachment.htm>


More information about the DOMjudge-devel mailing list