Ask Your Question
0

Jersey JAVA REST Client giving Error 500 “BAD Request” for POST request, while POSTMAN is able POST to same SDC REST API

asked 2018-01-17 07:09:56 -0600

Vijay Shekhawat gravatar image

updated 2018-01-18 02:56:23 -0600

I am trying to post form data through a JAVA Jersey REST client but i receive the response code 500 and an according exception:

java.lang.RuntimeException: Failed with HTTP error code : 500

The same request from POSTMAN(Chrome Extention) works successfully.

I am making a POST request to StreamSets Data Collector API.

My POM.xml is

   <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
 instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-
 4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vijay</groupId>
<artifactId>mavenproject2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
        <jersey.version>2.12</jersey.version>
<jersey.scope>compile</jersey.scope>
<compileSource>1.7</compileSource>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Jersey core Servlet 2.x implementation -->
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>${jersey.version}</version>
    <scope>${jersey.scope}</scope>
</dependency>

<!-- Jersey Multipart entity providers support module -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>${jersey.version}</version>
    <scope>${jersey.scope}</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<version>2.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Below is my Code

import java.io.File;
import java.util.Base64;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.client.Client
import java.security.KeyManagementException
import java.security.NoSuchAlgorithmException;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;

public static String testUploadService(String httpURL, File filePath)  throws Exception {

// local variables
ClientConfig clientConfig = null;
Client client = null;
WebTarget webTarget = null;
Invocation.Builder invocationBuilder = null;
Response response = null;
FileDataBodyPart fileDataBodyPart = null;
FormDataMultiPart formDataMultiPart = null;
int responseCode;
String responseMessageFromServer = null;
String responseString = null;
  String name = "*******";
String password = "*******";
String authString = name + ":" + password;
String sdc="sdc";
byte[] encoding = Base64.getEncoder().encode(authString.getBytes());
byte[] encoding2 = Base64.getEncoder().encode(sdc.getBytes());
String USER_PASS = new String(encoding);
String auth2=new String(encoding2);

try{

    ClientConfig cc = new ClientConfig();
    cc.register(MultiPartFeature.class);

    try {
    client = new JerseywithSSL().initClient(cc);
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) { 
    e.printStackTrace();
    }
    webTarget = client.target(httpURL);

    // set file upload values
    fileDataBodyPart = new FileDataBodyPart("uploadFile", filePath, MediaType.MULTIPART_FORM_DATA_TYPE);
    formDataMultiPart = new FormDataMultiPart();
    formDataMultiPart.bodyPart(fileDataBodyPart);

    // invoke service
    invocationBuilder = webTarget.request();
              invocationBuilder.header("Authorization", "Basic " + USER_PASS);
              invocationBuilder.header("X-Requested-By","SDC"); //Additional Header requiered by Streamsets RestAPI
             invocationBuilder.header("Content-type", "multipart/form-data");
    response = invocationBuilder.post(Entity.entity(formDataMultiPart, MediaType.MULTIPART_FORM_DATA));

    // get response code
    responseCode = response.getStatus();
    System.out.println("Response code: " + responseCode);

    if (response.getStatus() != 200) {
        throw new RuntimeException("Failed with HTTP error code : " + responseCode);
    }

    // get response message ...
(more)
edit retag flag offensive close merge delete

Comments

Can you add your imports? It's really hard to compile and run your example without them.

metadaddy gravatar imagemetadaddy ( 2018-01-17 13:46:25 -0600 )edit

@metadaddy Please have a look at updated question, I have included the all the imports and error stack trace .

Vijay Shekhawat gravatar imageVijay Shekhawat ( 2018-01-18 02:55:03 -0600 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2018-01-17 12:46:55 -0600

jeff gravatar image

To clarify, you are writing some code that will post to the SDC API? The most straightforward way to see what's going wrong is to check the sdc.log on the SDC instance that is handling the request. Failing that, you can probably enable logging in your client code to show the full response headers/payload, which should shed some more light on what the server reports as being the error. Here is how you do that in Jersey, for example (it's not clear to me what client you're using from the snippet above).

edit flag offensive delete link more

Comments

Yes, I am Indeed trying to POST pipelines to SDC API and I am using Jersey for the same. Have a look at my updated question.

Vijay Shekhawat gravatar imageVijay Shekhawat ( 2018-01-18 02:55:32 -0600 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-01-17 07:09:56 -0600

Seen: 74 times

Last updated: Jan 18