gpt4 book ai didi

java - 使用 Google API 中的 com.google.api.client.http.HttpRequest 对象发送 POST 请求

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:55:02 25 4
gpt4 key购买 nike

我必须发送具有以下结构的 POST 请求。

    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
}

我正在使用下面的代码发送 POST 请求,但我收到的响应是“400 错误请求”

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

我想知道是否有从事过类似任务的人可以帮助我根据问题开头提到的 POST 请求格式发送 POST 请求。

最佳答案

我已经使用下面的代码发送了 POST 请求

String requestBody = "{'name': 'newIndia','columns': [{'name': 'Species','type': 'STRING'}],'description': 'Insect Tracking Information.','isExportable': true}";
HttpRequest request = requestFactory.buildPostRequest(url, ByteArrayContent.fromString("application/json", requestBody));
request.getHeaders().setContentType("application/json");

关于java - 使用 Google API 中的 com.google.api.client.http.HttpRequest 对象发送 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14751115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com