简体   繁体   中英

Post multipart form with google-http-java-client

It's not clear from the google-http-java-client* docs how you would go about posting a form that has a 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 doesn't work. The API returns the error "Printer Id required for this request." which seems to me like the request isn't properly formed.

What am I doing wrong?

* I'm specifically using the google-http-java-client as it handles automatic refreshing of OAuth tokens etc for me. Please don't reply with solutions that involve using other HTTP clients.

So it looks like I misunderstood how form fields are added to multipart messages. The working code now looks like this

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) {
    ...
}

The most important parts above were overriding the default HttpMediaType to specify "multipart/form-data" and adding each field as its own part with a "Content-Disposition" header to designate the form field name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM