8
Muszę wysłać żądanie POST o następującej strukturze.Wysyłanie żądania POST przy użyciu com.google.api.client.http.HttpRequest obiektu w Google API
POST https://www.googleapis.com/fusiontables/v1/tables
Authorization: /* auth token here */
Content-Type: application/json
{
"name": "Insects",
"columns": [
{
"name": "Species",
"type": "STRING"
},
{
"name": "Elevation",
"type": "NUMBER"
},
{
"name": "Year",
"type": "DATETIME"
}
],
"description": "Insect Tracking Information.",
"isExportable": true
}
Używam poniżej kod, aby wysłać żądanie POST ale otrzymuję odpowiedź jako „400 Bad Request”
String PostUrl = "https://www.googleapis.com/fusiontables/v1/tables";
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(credential);
//generate the REST based URL
GenericUrl url = new GenericUrl(PostUrl.replaceAll(" ", "%20"));
//make POST request
String requestBody = "{'name': 'newIndia','columns': [{'name': 'Species','type': 'STRING'}],'description': 'Insect Tracking Information.','isExportable': true}";
HttpRequest request = requestFactory.buildPostRequest(url, ByteArrayContent.fromString(null, requestBody));
request.getHeaders().setContentType("application/json");
// Google servers will fail to process a POST/PUT/PATCH unless the Content-Length
// header >= 1
//request.setAllowEmptyContent(false);
System.out.println("HttpRequest request" + request);
HttpResponse response = request.execute();
Zastanawiam się, czy ktoś tam, którzy pracowali nad tym podobnego zadania może pomóc wysyłać żądanie POST zgodnie z formatem żądania POST, jak wspomniano na początku tego pytania.
Właściwie miałem zastąpić pierwszy argument 'ByteArrayContent.fromString' z' null', w celu uzyskania prawidłowego żądania HTTP. 'requestFactory.buildPostRequest (url, ByteArrayContent.fromString (null, requestBody));' – Davincho
Dla 'application/x-www-form-urlencoded' użyj' com.google.api.client.http.UrlEncodedContent' – Kalem