向外部 API 发送 post 请求并提取数据
sending a post request to an external API and extracting data
我希望在我的代码中模仿以下 post 请求:
curl -v -H "Accept: application/json" \
-H "Content-type: application/json" \
-H "App-id: $APP_ID" \
-H "Secret: $SECRET" \
-X POST \
-d "{ \
\"data\": { \
\"identifier\": \"test1\" \
} \
}" \
https://www.sampleurl.com/createuser
理想情况下,我得到的 JSON 响应应该是这样的
{
"data": {
"id": "111",
"identifier": "test1",
"secret": "SECRET"
}
}
我正在尝试使用 WebClient 来构建这样的请求
WebClient req = WebClient.builder().baseUrl("https://www.sampleurl.com").build();
String data = "{" + "\n" + "\t" + "\"data\": { \n";
data += "\t" + "\t" + "\"identifier\": \"" + username + "\"\n";
data += "\t" + "}" + "\n" + "}";
body.setIdentifier(username);
String t = req.post().uri("/createuser")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.header("App-id", APPID)
.header("Secret", SECRET)
.body(BodyInserters.fromPublisher(Mono.just(data), String.class))
.retrieve()
.bodyToMono(String.class)
.doOnNext(myString -> {System.out.println(myString);})
.block();
我遇到错误
org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest: 400 Bad Request
这样做...我哪里错了?还有更有效的发送此类请求的方法吗?我无法理解如何正确使用 Mono。
创建实体 class 并将其作为对象发送
class RequestPayloadData {
private String identifier;
//..getters and setters (or lombok annotation on the class)
}
class RequestPayload {
private RequestPayloadData data;
//..getters and setters (or lombok annotation on the class)
}
WebClient req = WebClient.builder().baseUrl("https://www.sampleurl.com").build();
RequestPayload data = new RequestPayload();
data.setData(new RequestPayloadData("test1"));
String t = req.post().uri("/createuser")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.header("App-id", APPID)
.header("Secret", SECRET)
.body(BodyInserters.fromPublisher(Mono.just(data), RequestPayload.class))
.retrieve()
.bodyToMono(String.class)
.doOnNext(myString -> {System.out.println(myString);})
.block();
我希望在我的代码中模仿以下 post 请求:
curl -v -H "Accept: application/json" \
-H "Content-type: application/json" \
-H "App-id: $APP_ID" \
-H "Secret: $SECRET" \
-X POST \
-d "{ \
\"data\": { \
\"identifier\": \"test1\" \
} \
}" \
https://www.sampleurl.com/createuser
理想情况下,我得到的 JSON 响应应该是这样的
{ "data": { "id": "111", "identifier": "test1", "secret": "SECRET" } }
我正在尝试使用 WebClient 来构建这样的请求
WebClient req = WebClient.builder().baseUrl("https://www.sampleurl.com").build();
String data = "{" + "\n" + "\t" + "\"data\": { \n";
data += "\t" + "\t" + "\"identifier\": \"" + username + "\"\n";
data += "\t" + "}" + "\n" + "}";
body.setIdentifier(username);
String t = req.post().uri("/createuser")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.header("App-id", APPID)
.header("Secret", SECRET)
.body(BodyInserters.fromPublisher(Mono.just(data), String.class))
.retrieve()
.bodyToMono(String.class)
.doOnNext(myString -> {System.out.println(myString);})
.block();
我遇到错误
org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest: 400 Bad Request
这样做...我哪里错了?还有更有效的发送此类请求的方法吗?我无法理解如何正确使用 Mono。
创建实体 class 并将其作为对象发送
class RequestPayloadData {
private String identifier;
//..getters and setters (or lombok annotation on the class)
}
class RequestPayload {
private RequestPayloadData data;
//..getters and setters (or lombok annotation on the class)
}
WebClient req = WebClient.builder().baseUrl("https://www.sampleurl.com").build();
RequestPayload data = new RequestPayload();
data.setData(new RequestPayloadData("test1"));
String t = req.post().uri("/createuser")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.header("App-id", APPID)
.header("Secret", SECRET)
.body(BodyInserters.fromPublisher(Mono.just(data), RequestPayload.class))
.retrieve()
.bodyToMono(String.class)
.doOnNext(myString -> {System.out.println(myString);})
.block();