2012-07-02 70 views
1

我試圖使用Java Apache HttpClient發送多部分POST請求,以將某些測試數據導入到BigQuery表中,但我一直收到HTTP 400錯誤消息「Invalid Upload Request 」。我很確定我很好地遵循BigQuery教程來重現所需,但不起作用。有人可以看看這個請求,看看它是否有什麼問題嗎?POST中的「無效上傳請求」

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來( 「內容類型:」 .. ),錯誤更改爲「Media type'text/plain'不支持。有效媒體類型:[application/octet-stream]」。

+0

我在使用node.js執行多部分文章時遇到同樣的問題。你有沒有發現問題是什麼? – Gus

+0

@Gus:不,不幸的是我沒有,而且我已經開始了另一個項目,所以我不再追求這個問題。我希望你找到/找到了解決方案! –

回答

0

在Content-Type標頭後應該有一個額外的換行符。

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