1

我正在尝试使用 Java Apache HttpClient 发送多部分 POST 请求以将一些测试数据导入 BigQuery 表,但我不断收到 HTTP 400 错误消息“无效上传请求”。我很确定我已经很好地遵循了 BigQuery 教程来重现所需的内容,但它不起作用。有人可以看看这个请求,看看它有什么问题吗?

    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--";

然后像这样执行请求:

    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);

此外,如果我将 addHeader("Content-Type"...) 更改为 addHeader("Content-Type:"...),则错误更改为“不支持媒体类型 'text/plain'。有效的媒体类型:[应用程序/八位字节流]”。

4

1 回答 1

0

在 Content-Type 标头之后应该有一个额外的换行符。

rawInput += "Content-Type: application/json; charset=UTF-8\n\n";
于 2012-08-14T17:49:44.393 回答