我想进行类似于以下 curl 命令的 API 调用:
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer
1djCb/mXV+KtryMxr6i1bXw"
-d '{"operands":[]}'
https://ads.line.me/api/v1.0/authority_delegations/get
我在尝试什么
public void send_deligation_request(String details[]) throws Exception{
System.out.println(Arrays.toString(details));
URL line_api_url = new URL("https://ads.line.me/api/v1.0/authority_delegations/get");
String payload = "{operands:[]}";
HttpURLConnection linec = (HttpURLConnection)line_api_url.openConnection();
linec.setDoInput(true);
linec.setDoOutput(true);
linec.setRequestMethod("POST");
linec.setRequestProperty("Content-Type", "application/json");
linec.setRequestProperty("Authorization", "Bearer "+access_token);
OutputStreamWriter writer = new OutputStreamWriter(linec.getOutputStream(), "UTF-8");
writer.write(payload);
BufferedReader in = new BufferedReader(
new InputStreamReader(
linec.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
但我遇到以下错误:
[naofumi.haida@torchlight.co.jp, 5514]
java.io.IOException: Server returned HTTP response code: 400 for URL: https://ads.line.me/api/v1.0/authority_delegations/get at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) at AuthorityDelegation.send_deligation_request(AuthorityDelegation.java:66) at AuthorityDelegation.read_csv(AuthorityDelegation.java:36) at AuthorityDelegation.main(AuthorityDelegation.java:20)
有人可以帮帮我吗?
最佳答案
HTTP 代码 400 表示错误请求。
我无法访问您共享的端点,但这是我用于演示的免费在线 REST API ..
curl -X POST \
https://jsonplaceholder.typicode.com/posts \
-H 'cache-control: no-cache' \
-H 'postman-token: 907bbf75-73f5-703f-c8b6-3e1cd674ebf7' \
-d '{
"userId": 100,
"id": 100,
"title": "main title",
"body": "main body"
}'
-H
= 标题-d
= 数据
示例运行:
[/c]$ curl -X POST \
> https://jsonplaceholder.typicode.com/posts \
> -H 'cache-control: no-cache' \
> -H 'postman-token: 907bbf75-73f5-703f-c8b6-3e1cd674ebf7' \
> -d '{
> "userId": 100,
> "id": 100,
> "title": "main title",
> "body": "main body"
> }'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 258 100 150 100 108 147 106 0:00:01 0:00:01 --:--:-- 192{
"{\n \"userId\": 100,\n \"id\": 100,\n \"title\": \"main title\",\n \"body\": \"main body\"\n }": "",
"id": 101
}
Java代码如下:
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body = RequestBody.create(mediaType, "{\n \"userId\": 100,\n \"id\": 100,\n \"title\": \"main title\",\n \"body\": \"main body\"\n }");
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts")
.post(body)
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "e11ce033-931a-0419-4903-ab860261a91a")
.build();
Response response = client.newCall(request).execute();
另一个用数据调用 REST POST 的例子..
User user = new User();
user.setFirstName("john");
user.setLastName("Maclane");
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("URL");
Response response = target.request().post(Entity.entity(user, <MEDIATYPE>));
//Read output in string format
System.out.println(response.getStatus());
response.close();
这是我用我的端点和负载更新代码后的样子。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
public class TestClass {
public static final String POST_URL = "https://jsonplaceholder.typicode.com/posts";
public static final String POST_DATA = "{\"userId\": 100,\"id\": 100,\"title\": \"main title\",\"body\": \"main body\"}";
public static void main(String[] args) throws Exception {
String[] details = {};
System.out.println(Arrays.toString(details));
URL line_api_url = new URL(POST_URL);
String payload = POST_DATA;
HttpURLConnection linec = (HttpURLConnection) line_api_url
.openConnection();
linec.setDoInput(true);
linec.setDoOutput(true);
linec.setRequestMethod("POST");
linec.setRequestProperty("Content-Type", "application/json");
linec.setRequestProperty("Authorization", "Bearer "
+ "1djCb/mXV+KtryMxr6i1bXw");
OutputStreamWriter writer = new OutputStreamWriter(
linec.getOutputStream(), "UTF-8");
writer.write(payload);
BufferedReader in = new BufferedReader(new InputStreamReader(
linec.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
简而言之,检查 API 文档并确保请求负载的格式正确,因为 400 表示 BAD REQUEST。
关于Java:如何使用数据进行 API 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46233770/