1
votes

I'm trying to use the Java Apache HttpClient to send a multipart POST request to import some test data into a BigQuery table, but I keep getting HTTP 400 errors with the message "Invalid Upload Request". I'm pretty sure I followed the BigQuery tutorial well enough to reproduce what's needed, but it's not working. Could someone take a look at this request and see if anything is wrong with it?

    String rawInput = "--xxx\n";
    rawInput += "Content-Type: application/json; charset=UTF-8\n";
    rawInput += "{\n";
    rawInput += "'configuration': {\n"; 
    rawInput += "'load': {\n";
    rawInput += "'schema': {\n";
    rawInput += "'fields': [\n";
    rawInput += "{'name':'field1', 'type':'STRING'},\n";
    rawInput += "{'name':'field2', 'type':'STRING'},\n";
    rawInput += "{'name':'field3', 'type':'STRING'},\n";
    rawInput += "{'name':'field4', 'type':'STRING'},\n";
    rawInput += "]\n";
    rawInput += "},\n";
    rawInput += "'destinationTable': {\n";
    rawInput += "'projectId': 'xxxxxxxxxxx [redacted]',\n";
    rawInput += "'datasetId': 'test',\n";
    rawInput += "'tableId': 'test'\n";
    rawInput += "}\n";
    rawInput += "createDisposition = CREATE_IF_NEEDED\n";           
    rawInput += "}\n";
    rawInput += "}\n";
    rawInput += "}\n";
    rawInput += "--xxx\n";
    rawInput += "Content-Type: application/octet-stream\n";
    rawInput += "\n";
    rawInput += "1,1234,11111111,1\n";
    rawInput += "2,5678,11111111,1\n";
    rawInput += "3,9101,11111111,1\n";
    rawInput += "4,6543,11111111,1\n";
    rawInput += "--xxx--";

The request is then executed like so:

    postRequest.addHeader("Content-Type", "multipart/related; boundary=xxx;");
    postRequest.addHeader("Authorization", "OAuth " + credential.getAccessToken());
    StringEntity input = new StringEntity(rawInput);
    postRequest.setEntity(input);
    HttpResponse response = httpClient.execute(postRequest);

Also, if I change the addHeader("Content-Type"...) to addHeader("Content-Type:"...), the error changes to "Media type 'text/plain' is not supported. Valid media types: [application/octet-stream]".

1
I am getting this same problem while doing a multipart post using node.js. Have you found what the problem was?Gus
@Gus: No, unfortunately I haven't, and I've moved on to a different project, so I'm no longer pursuing this issue. I hope you find/have found a solution though!Stanley Ayzenberg

1 Answers

0
votes

There should be an additional newline after the Content-Type header.

rawInput += "Content-Type: application/json; charset=UTF-8\n\n";