engg_nayan | Posts: 34

400 code error

0 votes
Hi, I get 400 error code. I have the sample code below:package RestAPI; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.nio.file.Files; import esignLiv.com.Utility; public class SimpleCreateAndSendREST { public static void main(String[] args) throws MalformedURLException, IOException { //System.setProperty("javax.net.ssl.trustStore","C:/Users/pl29158/myprojects/meao/srv/config/cacerts"); String requestURL = Utility.BASE_URL; String apiKey = Utility.API_KEY; String charset = "UTF-8"; File uploadFile1 = new File("C:/Users/pl29158/doc/sample_file2.pdf"); String boundary = Long.toHexString(System.currentTimeMillis()); // Generate a random value for the form boundary String CRLF = "\r\n"; // Line separator used in multipart/form-data. String jsonContent = "{\"roles\":[{\"locked\":false,\"emailMessage\":{\"content\":\"\"},\"attachmentRequirements\":[],\"reassign\":false,\"specialTypes\":[],\"id\":\"Sender\",\"data\":null,\"type\":\"SIGNER\",\"index\":0,\"signers\":[{\"auth\":{\"challenges\":[],\"scheme\":\"NONE\"},\"company\":\"Silanis\",\"firstName\":\"Nuru\",\"lastName\":\"Jaman\",\"phone\":\"\",\"email\":\"[email protected]\",\"knowledgeBasedAuthentication\":null,\"language\":\"en\",\"title\":\"Silanis\",\"external\":null,\"professionalIdentityFields\":[],\"userCustomFields\":[],\"delivery\":{\"email\":true,\"provider\":false,\"download\":true},\"group\":null,\"signature\":null,\"address\":null,\"data\":null,\"name\":\"\",\"specialTypes\":[]}],\"name\":\"Sender\"},{\"locked\":false,\"emailMessage\":{\"content\":\"\"},\"attachmentRequirements\":[],\"reassign\":false,\"specialTypes\":[],\"id\":\"Signer\",\"data\":null,\"type\":\"SIGNER\",\"index\":0,\"signers\":[{\"auth\":{\"challenges\":[],\"scheme\":\"NONE\"},\"company\":\"PNC\",\"firstName\":\"Nayan\",\"SignerLast\":\"Silanis\",\"phone\":\"\",\"email\":\"[email protected]\",\"knowledgeBasedAuthentication\":null,\"language\":\"en\",\"title\":\"\",\"external\":null,\"professionalIdentityFields\":[],\"userCustomFields\":[],\"delivery\":{\"email\":false,\"provider\":false,\"download\":false},\"group\":null,\"id\":\"Signer\",\"signature\":null,\"address\":null,\"data\":null,\"name\":\"\",\"specialTypes\":[]}],\"name\":\"Signer\"}],\"documents\": [{\"approvals\":[{\"role\":\"Signer\",\"signed\":null,\"accepted\":null,\"data\":null,\"fields\":[{\"page\":0,\"subtype\":\"FULLNAME\",\"width\":200,\"binding\":null,\"extract\":false,\"extractAnchor\":null,\"left\":175,\"top\":165,\"validation\":null,\"height\":50,\"data\":null,\"type\":\"SIGNATURE\",\"value\":\"\"}],\"name\":\"\"},{\"role\":\"Sender\",\"signed\":null,\"accepted\":null,\"data\":null,\"fields\":[{\"page\":0,\"subtype\":\"FULLNAME\",\"width\":200,\"binding\":null,\"extract\":false,\"extractAnchor\":null,\"left\":550,\"top\":165,\"validation\":null,\"height\":50,\"data\":null,\"type\":\"SIGNATURE\",\"value\":\"\"}],\"name\":\"\"}],\"name\": \"sampleAgreement\"}],\"name\": \"Test Package REST\", \"type\":\"PACKAGE\", \"language\":\"en\", \"emailMessage\":\"\", \"description\":\"New Package\",\"autoComplete\":true,\"status\":\"SENT\"}"; //String jsonContent="{\"name\": \"My Document\",\"type\":\"PACKAGE\",\"language\":\"en\",\"emailMessage\":\"\",\"description\":\"New Package\",\"autocomplete\":true}"; URLConnection connection = new URL(requestURL+"/packages/").openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); connection.setRequestProperty("Authentication", "Required"); connection.setRequestProperty("Authorization", "Basic " + apiKey); connection.setRequestProperty("Accept", "application/json"); OutputStream output = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true); try { // Add pdf file. writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + uploadFile1.getName() + "\"").append(CRLF); writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(uploadFile1.getName())).append(CRLF); writer.append("Content-Transfer-Encoding: application/pdf").append(CRLF); writer.append(CRLF).flush(); Files.copy(uploadFile1.toPath(), output); output.flush(); writer.append(CRLF).flush(); // add json payload writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"payload\"").append(CRLF); writer.append("Content-Type: application/json; charset=" + charset).append(CRLF); writer.append(CRLF).append(jsonContent).append(CRLF).flush(); // End of multipart/form-data. writer.append("--" + boundary + "--").append(CRLF).flush(); } catch (IOException ex) { System.err.println(ex); } //get and write out response code int responseCode = ((HttpURLConnection) connection).getResponseCode(); System.out.println(responseCode); //get and write out response BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //print result System.out.println(response.toString()); } } package esignLiv.com; public class Utility { //sandbox public static String API_KEY = "=="; public static String BASE_URL = "https://sandbox.esignlive.com/api"; //pnc //public static String API_KEY = "=="; //public static String BASE_URL = "https://e-signature-uat.pnc.com/a/api"; } and the error below: 400 Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://sandbox.esignlive.com/api/packages/ at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1926) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1921) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1920) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1490) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) at RestAPI.SimpleCreateAndSendREST.main(SimpleCreateAndSendREST.java:71) Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://sandbox.esignlive.com/api/packages/ at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338) at RestAPI.SimpleCreateAndSendREST.main(SimpleCreateAndSendREST.java:67)

harishaidary | Posts: 1812

Reply to: 400 code error

1 votes
Hi there, There was an invalid field in your json payload. "SignerLast" is not a valid property in esignlive. Please change that to "lastName". Here's a valid working sample code with your payload:
String requestURL = "https://sandbox.esignlive.com/api";
String apiKey = "==";

String charset = "UTF-8";
File uploadFile1 = new File("path_to_your_pdf");
String boundary = Long.toHexString(System.currentTimeMillis()); // Generate a random value for the form boundary
String CRLF = "\r\n"; // Line separator used in multipart/form-data.
String jsonContent = "{\"roles\":[{\"locked\":false,\"emailMessage\":{\"content\":\"\"},\"attachmentRequirements\":[],\"reassign\":false,\"specialTypes\":[],\"id\":\"Sender\",\"data\":null,\"type\":\"SIGNER\",\"index\":0,\"signers\":[{\"auth\":{\"challenges\":[],\"scheme\":\"NONE\"},\"company\":\"Silanis\",\"firstName\":\"Nuru\",\"lastName\":\"Jaman\",\"phone\":\"\",\"email\":\"[email protected]\",\"knowledgeBasedAuthentication\":null,\"language\":\"en\",\"title\":\"Silanis\",\"external\":null,\"professionalIdentityFields\":[],\"userCustomFields\":[],\"delivery\":{\"email\":true,\"provider\":false,\"download\":true},\"group\":null,\"signature\":null,\"address\":null,\"data\":null,\"name\":\"\",\"specialTypes\":[]}],\"name\":\"Sender\"},{\"locked\":false,\"emailMessage\":{\"content\":\"\"},\"attachmentRequirements\":[],\"reassign\":false,\"specialTypes\":[],\"id\":\"Signer\",\"data\":null,\"type\":\"SIGNER\",\"index\":0,\"signers\":[{\"auth\":{\"challenges\":[],\"scheme\":\"NONE\"},\"company\":\"PNC\",\"firstName\":\"Nayan\",\"lastName\":\"Silanis\",\"phone\":\"\",\"email\":\"[email protected]\",\"knowledgeBasedAuthentication\":null,\"language\":\"en\",\"title\":\"\",\"external\":null,\"professionalIdentityFields\":[],\"userCustomFields\":[],\"delivery\":{\"email\":false,\"provider\":false,\"download\":false},\"group\":null,\"id\":\"Signer\",\"signature\":null,\"address\":null,\"data\":null,\"name\":\"\",\"specialTypes\":[]}],\"name\":\"Signer\"}],\"documents\":[{\"approvals\":[{\"role\":\"Signer\",\"signed\":null,\"accepted\":null,\"data\":null,\"fields\":[{\"page\":0,\"subtype\":\"FULLNAME\",\"width\":200,\"binding\":null,\"extract\":false,\"extractAnchor\":null,\"left\":175,\"top\":165,\"validation\":null,\"height\":50,\"data\":null,\"type\":\"SIGNATURE\",\"value\":\"\"}],\"name\":\"\"},{\"role\":\"Sender\",\"signed\":null,\"accepted\":null,\"data\":null,\"fields\":[{\"page\":0,\"subtype\":\"FULLNAME\",\"width\":200,\"binding\":null,\"extract\":false,\"extractAnchor\":null,\"left\":550,\"top\":165,\"validation\":null,\"height\":50,\"data\":null,\"type\":\"SIGNATURE\",\"value\":\"\"}],\"name\":\"\"}],\"name\":\"sampleAgreement\"}],\"name\":\"Test Package REST\",\"type\":\"PACKAGE\",\"language\":\"en\",\"emailMessage\":\"\",\"description\":\"New Package\",\"autoComplete\":true,\"status\":\"SENT\"}";
URLConnection connection = new URL(requestURL + "/packages/").openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
connection.setRequestProperty("Authorization", "Basic " + apiKey);
connection.setRequestProperty("Accept", "application/json");
    OutputStream output = connection.getOutputStream();
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
 
        try  {
 
             // Add pdf file.
     writer.append("--" + boundary).append(CRLF);
     writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + uploadFile1.getName() + "\"").append(CRLF);
     writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(uploadFile1.getName())).append(CRLF);
     writer.append("Content-Transfer-Encoding: application/pdf").append(CRLF);
     writer.append(CRLF).flush();
     Files.copy(uploadFile1.toPath(), output);
     output.flush();
     writer.append(CRLF).flush();
         
     // add json payload
     writer.append("--" + boundary).append(CRLF);
     writer.append("Content-Disposition: form-data; name=\"payload\"").append(CRLF);
     writer.append("Content-Type: application/json; charset=" + charset).append(CRLF);
             writer.append(CRLF).append(jsonContent).append(CRLF).flush();
 
             // End of multipart/form-data.
     writer.append("--" + boundary + "--").append(CRLF).flush();
        }
        catch (IOException ex) {
            System.err.println(ex);
        }
 
        //get and write out response code
int responseCode = ((HttpURLConnection) connection).getResponseCode();
System.out.println(responseCode);
 
//get and write out response
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
 
while ((inputLine = in.readLine()) != null) {
     response.append(inputLine);
}
    in.close();
  
//print result
System.out.println(response.toString());

engg_nayan | Posts: 34

Reply to: 400 code error

0 votes
Thanks a lot. It's working now.

Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off