如何参考API的shell命令向API发出POST请求?
How can I make a POST request to the API with reference to the shell command of API?
我正在开发 Android 应用程序,该应用程序使用 API of HyperTrack(这是一项网络服务,提供 API 用于跟踪移动设备real-time。它还在单独的 API 中提供任务和 Driver 处理的功能。)
Start a Trip 功能要求我使用下面的 shell 命令获取 Driver 密钥:
Driver API:
curl -H "Authorization: token YOUR_SK_TOKEN" \
-H "Content-Type: application/json" \
-X POST \
-d "{\"name\": \"Test driver\", \"vehicle_type\": \"car\"}" \
https://app.hypertrack.io/api/v1/drivers/
这就是我使用带有以下请求接口的 Retrofit2 实现这两个 API 的方式:
public interface DriverRequestInterface
{
@Headers ({
"Authorization: token SECRET_KEY",
"Content-Type: application/json"
})
@POST ( "api/v1/drivers" )
Call<DriverJSONResponse> getJSON (@Body DriverJSONResponse jsonResponse);
}
这是我的DriverJSON回复:
public class DriverJSONResponse
{
/*
JSON Object Fields
@SerializedName ( "count" ) private int count;
*/
@SerializedName ( "name" ) private String name;
@SerializedName ( "vehicle_type" ) private String vehicleType;
public DriverJSONResponse(String name, String vehicleType)
{
this.name = name;
this.vehicleType = vehicleType;
}
/*
Setter and Getter of JSON Object Fields
public void setCount (int count) { this.count = count; }
public int getCount () { return count; }
*/
}
到目前为止,我收到的是 GET 响应,而不是 POST。我收到 JSON Object 和结果列表,但无法 post 任何 API。
如何参考API的shell命令向API发出POST请求?
由于 DriverJSONResponse
class 包含其他字段以及 name
和 vehicle_type
,上面的代码将以下数据传递给 POST
:
{"count":0, "name":"Brian", "vehicle_type":"car", "results":[] }
这会导致 JSON 解析错误。
因此,使用另一个模型 class 来传递 POST
参数,例如:
public class DriverJSON
{
@SerializedName ( "name" ) private String name;
@SerializedName ( "vehicle_type" ) private String vehicleType;
public DriverJSON(String name, String vehicleType)
{
this.name = name;
this.vehicleType = vehicleType;
}
public String getName () { return name; }
public String getVehicleType () { return vehicleType; }
}
并在 RequestInterface
中传递此模型 class,例如:
public interface DriverRequestInterface
{
@Headers ({
"Authorization: token YOUR_SECRET_KEY",
"Content-Type: application/json"
})
@POST ( "api/v1/drivers/" )
Call<DriverJSONResponse> getJSON (@Body DriverJSON json);
}
并且不要忘记根据您希望收到的 JSON 对象为您的 DriverJSONResponse
建模。
我正在开发 Android 应用程序,该应用程序使用 API of HyperTrack(这是一项网络服务,提供 API 用于跟踪移动设备real-time。它还在单独的 API 中提供任务和 Driver 处理的功能。)
Start a Trip 功能要求我使用下面的 shell 命令获取 Driver 密钥:
Driver API:
curl -H "Authorization: token YOUR_SK_TOKEN" \
-H "Content-Type: application/json" \
-X POST \
-d "{\"name\": \"Test driver\", \"vehicle_type\": \"car\"}" \
https://app.hypertrack.io/api/v1/drivers/
这就是我使用带有以下请求接口的 Retrofit2 实现这两个 API 的方式:
public interface DriverRequestInterface
{
@Headers ({
"Authorization: token SECRET_KEY",
"Content-Type: application/json"
})
@POST ( "api/v1/drivers" )
Call<DriverJSONResponse> getJSON (@Body DriverJSONResponse jsonResponse);
}
这是我的DriverJSON回复:
public class DriverJSONResponse
{
/*
JSON Object Fields
@SerializedName ( "count" ) private int count;
*/
@SerializedName ( "name" ) private String name;
@SerializedName ( "vehicle_type" ) private String vehicleType;
public DriverJSONResponse(String name, String vehicleType)
{
this.name = name;
this.vehicleType = vehicleType;
}
/*
Setter and Getter of JSON Object Fields
public void setCount (int count) { this.count = count; }
public int getCount () { return count; }
*/
}
到目前为止,我收到的是 GET 响应,而不是 POST。我收到 JSON Object 和结果列表,但无法 post 任何 API。
如何参考API的shell命令向API发出POST请求?
由于 DriverJSONResponse
class 包含其他字段以及 name
和 vehicle_type
,上面的代码将以下数据传递给 POST
:
{"count":0, "name":"Brian", "vehicle_type":"car", "results":[] }
这会导致 JSON 解析错误。
因此,使用另一个模型 class 来传递 POST
参数,例如:
public class DriverJSON
{
@SerializedName ( "name" ) private String name;
@SerializedName ( "vehicle_type" ) private String vehicleType;
public DriverJSON(String name, String vehicleType)
{
this.name = name;
this.vehicleType = vehicleType;
}
public String getName () { return name; }
public String getVehicleType () { return vehicleType; }
}
并在 RequestInterface
中传递此模型 class,例如:
public interface DriverRequestInterface
{
@Headers ({
"Authorization: token YOUR_SECRET_KEY",
"Content-Type: application/json"
})
@POST ( "api/v1/drivers/" )
Call<DriverJSONResponse> getJSON (@Body DriverJSON json);
}
并且不要忘记根据您希望收到的 JSON 对象为您的 DriverJSONResponse
建模。