测试一个接口,在postman里很容易就通过。其中body部分需要上传的参数是这样的

对应的程序里我是通过创建两个json实现的
import net.sf.json.JSONObject;
String sourceBody = new StringBuilder()
.append("{\"href\":\""+ fromFoundingSource +"\"}")
.toString();
String destinationBody = new StringBuilder()
.append("{\"href\":\""+ toFoundingSource +"\"}")
.toString();
String amountBody = new StringBuilder()
.append("{\"currency\":\""+ currency +"\",")
.append("\"value\":\""+ money +"\"}")
.toString();
JSONObject linksJson = new JSONObject();
linksJson.put("source", sourceBody);
linksJson.put("destination", destinationBody);
JSONObject json = new JSONObject();
json.put("_links", linksJson.toString());
json.put("amount", amountBody);
最后将把json作为body参数设置到请求中,完成。

本文介绍了如何在程序中构造JSON对象,以模拟Postman中通过的接口请求。通过创建多个StringBuilder实例,构建了sourceBody, destinationBody和amountBody,然后将它们整合到一个JSONObject中,最终作为HTTP请求的body参数。这种方法对于处理复杂请求结构非常实用。

2728

被折叠的 条评论
为什么被折叠?



