如何使用应用程序接口参考
在本节中,我们将探讨交互示例以及如何在应用程序接口中使用参数
本示例中使用的所有数据均为占位符,仅供演示之用。在使用 API 时,请务必使用您自己的有效数据。
终点:
PATCHhttps://mobile.proxy-seller.com/api/v1/hosts/{hostId}/changeName
仔细观察
PATCH - 表示查询所使用的方法
{hostId} - 表示要替换的变量的有效值。
路径参数
名称 | 类型 | 说明 |
|---|---|---|
hostId* | 字符串 | id的主机 |
页眉
名称 | 价值 |
|---|---|
内容类型 |
|
授权* |
|
请求正文
名称 | 类型 | 说明 |
|---|---|---|
名字* | 字符串 | 新主机名 |
所有必选参数都标有星号 (*)
示例
https://mobile.proxy-seller.com/api/v1/hosts/21vjv1224n45v7457mvcas/changeName查询可以有不同的参数集和参数类型,见下图
让我们看一个在多种编程语言中使用的例子
curl -X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: YOUR_API_KEY" \
-d '{"name":"Host new name"}' \
"https://mobile.proxy-seller.com/api/v1/hosts/ID_OF_SOME_HOST/changeName"
User
import fetch from 'node-fetch';
const apiKey = "YOUR_API_KEY";
const hostId = "ID_OF_SOME_HOST";
// add path parameter to request
const url = "https://mobile.proxy-seller.com/api/v1/hosts/" + hostId + "/changeName";
// also "query" parameters are added here
// const urlWithQuery = url + "?someNameParameter=someValue"
const body = {
name: "Host new name",
// write here your body params
};
const response = await fetch(url, {
method: 'patch',
// add headers parameters to request
headers: {
'Content-Type': 'application/json', // it is impotent to body parameters
'Authorization': apiKey,
// write here your other headers params
},
// add body parameters to request
body: JSON.stringify(body)
});
console.log(await response.json()); try {
String apiKey = "YOUR_API_KEY";
String hostId = "ID_OF_SOME_HOST";
String url = "https://mobile.proxy-seller.com/api/v1/hosts/" + hostId + "/changeName";
// also "query" parameters are added here
// String urlWithQuery = url + "?someNameParameter=someValue"
// Constructing the request body
String body = "{\"name\": \"Host new name"}";
// Creating URL object
URL obj = new URL(url);
// Creating HTTPURLConnection
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// Setting the request method
con.setRequestMethod("PATCH");
// Setting headers
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", apiKey);
// Enabling the connection to send and receive data
con.setDoOutput(true);
// Writing the request body
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(body);
wr.flush();
wr.close();
// Reading the response
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Printing the response
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}按 HTTP 状态代码显示的响应示例。200 "表示成功,"4XX "表示错误。
回应
{
"id": "21vjv1224n45v7457mvcas",
"name": "Host new name",
"number": "101"
}AUTHORIZATION_FAILED,
IP_AUTHORIZATION_FAILED,
HOST_USER_ID_AND_AUTH_DOES_NOT_MATCHINVALID_HOST_ID,HOST_NOT_FOUND