noman404
noman404

Reputation: 940

Translate POSTMAN code into Java

I am trying to implement (in java) POSTMAN preview of POST request. it uses boundary and content-disposition, i am not so much familiar with boundary and content disposition implementation in java. I think experts can help me out from this problem, thanks in advance.

POST /etap-cgi/cgiunl.exe HTTP/1.1
Host: unl.ru
X-Auth-Token: 5dc347210229bd8b9565d213f0d4f80f738970083d03a8e73a0d5f649019272e
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="conversion"

true
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="language"

en
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="data"

aar partasi nah
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="outputmode"

text
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="coding"

utf-8
----WebKitFormBoundaryE19zNvXGzXaLvS5C

I have tried but it says'

Fatal error in process of translation

Here is my implementation in java.

HttpClient httpclient = new DefaultHttpClient();

    try {
        // url parameters
        MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
                .create();
        multipartEntity.addTextBody("conversion", "ture",
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("language", "en",
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("data", "example",
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("outputmode", "text",
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("coding", "utf-8",
                ContentType.TEXT_PLAIN);

        multipartEntity
                .setBoundary("----WebKitFormBoundaryE19zNvXGzXaLvS5C");


        HttpEntity multiPart = multipartEntity.build();

        HttpPost httpPost = new HttpPost(
                url);
        httpPost.setEntity(multiPart);
        httpPost.setHeader("Content-Disposition", "form-data");

        // get response after execution
        HttpResponse response = httpclient.execute(httpPost);
        // get response entities
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {
            System.out.println("Response content length: "
                    + resEntity.getContentLength());
            String responseBody = EntityUtils.toString(resEntity);
            System.out.println("Data: " + responseBody);
        }

        EntityUtils.consume(resEntity);

    } catch (Exception err) {
        System.err.println(err.toString());
    } finally {
        // close connection
        httpclient.getConnectionManager().shutdown();
    }

Upvotes: 1

Views: 11104

Answers (1)

noman404
noman404

Reputation: 940

I have solved my problem by myself with uploading a file instead of multi-part string, here is the soln with the boundary param without "WebKitFormBoundary" characters. hope this will helpful for someone.

public String httpOperation(String conversion, String fileName) {

    HttpClient httpclient = new DefaultHttpClient();
    try {
        // url parameters
        MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
                .create();
        // add html tags param
        multipartEntity.addTextBody("conversion", conversion,
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("language", EN, ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("outputmode", TEXT,
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("coding", UTF, ContentType.TEXT_PLAIN);

        // add files as attachments
        multipartEntity.addPart("sourcefile", new FileBody(new File(
                fileName), ContentType.TEXT_PLAIN, "filename"));

        multipartEntity.setBoundary(PARAM_BOUNDARY);

        HttpEntity postEntity = multipartEntity.build();

        HttpPost httpPost = new HttpPost(URL);
        httpPost.setHeader("Content-Disposition", "form-data;");
        httpPost.setEntity(postEntity);

        // get response after execution
        HttpResponse response = httpclient.execute(httpPost);

        // get response entities
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {
            @SuppressWarnings("deprecation")
            String responseBody = EntityUtils.toString(resEntity,
                    HTTP.UTF_8);
            // print output
            if (conversion.equals(TRUE)) {
                extractResponse(responseBody.toString());
                // System.out.println(responseBody.toString());
                result = responseBody.toString();
            } else {
                // System.out.println(responseBody.toString());
                result = responseBody.toString();
            }
        }
        EntityUtils.consume(resEntity);

    } catch (Exception err) {
        System.err.println(err.toString());
    } finally {
        // close connection
        httpclient.getConnectionManager().shutdown();
    }

    return result;
}

Here is the POSTMAN build preview

POST /url/ HTTP/1.1
Host: unl.ru
X-Auth-Token: 5dc347210229bd8b9565d213f0d4f80f738970083d03a8e73a0d5f649019272e
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="conversion"

false
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="language"

en
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="sourcefile";   filename="conversion.txt"
Content-Type: text/plain


----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="outputmode"

text
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="coding"

utf-8
----WebKitFormBoundaryE19zNvXGzXaLvS5C

Upvotes: 2

Related Questions