Publish a multi-page form using google-http-java-client

From the google-http-java-client documents, it’s not clear how you will place the form with the file field.

For example, I'm trying to print a document using the Google Cloud Print API:

HttpRequestFactory httpRequestFactory = getHttpRequestFactory();

Map<String, Object> parameters = Maps.newHashMap();
parameters.put("printerId", printRequest.getPrinterId());
parameters.put("title", printRequest.getTitle());
parameters.put("contentType", printRequest.getContentType());
parameters.put("ticket", new Gson().toJson(printRequest.getOptions()));

MultipartContent content = new MultipartContent();
content.addPart(new MultipartContent.Part(new UrlEncodedContent(parameters)));
content.addPart(new MultipartContent.Part(
        new FileContent(printRequest.getContentType(), printRequest.getFile())));

try {
    HttpResponse response = httpRequestFactory.buildPostRequest(
            SubmitUrl, content).execute();
    System.out.println(IOUtils.toString(response.getContent()));
} catch (IOException e) {
    String message = String.format();
    System.out.println("Error submitting print job: " + e.getMessage());
}

Unfortunately this will not work. The API returns the error "Printer ID required for this request." that it seems to me that the request is incorrectly formed.

What am I doing wrong?

* I specifically use google-http-java-client as it handles automatic updating of OAuth tokens, etc. for me. Do not respond to solutions related to using other HTTP clients.

+4
source share
1 answer

, , , .

HttpRequestFactory httpRequestFactory = getHttpRequestFactory(username);

Map<String, String> parameters = Maps.newHashMap();
parameters.put("printerid", printRequest.getPrinterId());
parameters.put("title", printRequest.getTitle());
parameters.put("contentType", printRequest.getContentType());

// Map print options into CJT structure
Map<String, Object> options = Maps.newHashMap();
options.put("version", "1.0");
options.put("print", printRequest.getOptions());
parameters.put("ticket", new Gson().toJson(options));

// Add parameters
MultipartContent content = new MultipartContent().setMediaType(
        new HttpMediaType("multipart/form-data")
                .setParameter("boundary", "__END_OF_PART__"));
for (String name : parameters.keySet()) {
    MultipartContent.Part part = new MultipartContent.Part(
            new ByteArrayContent(null, parameters.get(name).getBytes()));
    part.setHeaders(new HttpHeaders().set(
            "Content-Disposition", String.format("form-data; name=\"%s\"", name)));
    content.addPart(part);
}

// Add file
FileContent fileContent = new FileContent(
        printRequest.getContentType(), printRequest.getFile());
MultipartContent.Part part = new MultipartContent.Part(fileContent);
part.setHeaders(new HttpHeaders().set(
        "Content-Disposition", 
        String.format("form-data; name=\"content\"; filename=\"%s\"", printRequest.getFile().getName())));
content.addPart(part);

try {
    HttpResponse response = httpRequestFactory.buildPostRequest(
            SubmitUrl, content).execute();
    System.out.println(IOUtils.toString(response.getContent()));
} catch (IOException e) {
    ...
}

HttpMediaType "multipart/form-data" "Content-Disposition" .

+13

Source: https://habr.com/ru/post/1536019/


All Articles