英文:
How to pass a json object in OkHttp RequestBody for calling external Apis
问题
@Test
public void whenSendPostRequest_thenCorrect()
throws IOException {
RequestBody formBody = new FormBody.Builder()
.add("username", "test")
.add("password", "test")
.build();
Request request = new Request.Builder()
.url(BASE_URL + "/users")
.post(formBody)
.build();
Call call = client.newCall(request);
Response response = call.execute();
assertThat(response.code(), equalTo(200));
}
参考链接 - https://www.baeldung.com/guide-to-okhttp
我正在尝试执行这种类型的请求,其中在形成formBody时,我必须做类似于 .add("something", json_object)
这样的事情,但它接受一个字符串值,我不能传递JSON字符串,如何解决这个问题?
此外,json_object 非常大,因此无法为每个键值对添加许多 .add 语句。
英文:
@Test
public void whenSendPostRequest_thenCorrect()
throws IOException {
RequestBody formBody = new FormBody.Builder()
.add("username", "test")
.add("password", "test")
.build();
Request request = new Request.Builder()
.url(BASE_URL + "/users")
.post(formBody)
.build();
Call call = client.newCall(request);
Response response = call.execute();
assertThat(response.code(), equalTo(200));
}
Reference - https://www.baeldung.com/guide-to-okhttp
I am trying to execute this type of request where while forming formBody i have to do something like this to have .add("something", json_object) but it accepts a string value and I can't pass json string how to solve this issue
Also json_object is very large so can't just have many .add statements for each key value pair
答案1
得分: 0
建议您创建一个与JSON字符串中的字段相同的模型/POJO类,然后将该类类型的对象添加到FormBody类中。您可以使用JSON到对象映射器将输入的JSON转换为新模型类的对象。
英文:
Suggest you to create a model/pojo class which has the fields same as that in the json string. Then have an object of that class type added to the FormBody class. You can have a json to object mapper to convert the input json to an object of new model class.
答案2
得分: 0
以下是已翻译的内容:
所以你需要在这里进行集成测试。我正在使用类似这样的代码:
TestDto testDto = new TestDto(); // 你需要在这里设置字段变量
given()
.header(HttpHeaders.AUTHORIZATION, createJwtToken(ADMIN_ID))
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.ACCEPT_LANGUAGE, LANG_EN)
.contentType(ContentType.JSON)
.body(objectMapper.writeValueAsString(testDto))
.when()
.log().all()
.post(CONTROLLER_ENDPOINT)
.then()
.log().all()
.statusCode(HttpStatus.CREATED.value())
.extract()
.body().as(TestDto.class);
given() 可以在 rest-assured 中找到。
ObjectMapper 是一个可以在 jackson 库中找到的类:jackson-databind。createJwtToken() 负责生成具有 ADADMIN_ID 的 JWT。
希望这能帮到你。
英文:
So you need the Integration test here. I am using something like this:
TestDto testDto = new TestDto(); // you need to set the field variables here
given()
.header(HttpHeaders.AUTHORIZATION, createJwtToken(ADMIN_ID))
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.ACCEPT_LANGUAGE, LANG_EN)
.contentType(ContentType.JSON)
.body(objectMapper.writeValueAsString(testDto))
.when()
.log().all()
.post(CONTROLLER_ENDPOINT)
.then()
.log().all()
.statusCode(HttpStatus.CREATED.value())
.extract()
.body().as(TestDto.class);
given() will be found in rest-assured.
ObjectMapper is a class that can be found in a jackson library: jackson-databind. createJwtToken() is responsible to give a JWT with the ADADMIN_ID.
Hope, it will help.
答案3
得分: 0
After some exploration, I found out that we can convert json_string to requestbody JSON using the okhttp RequestBody class create method. This method takes JSON media type and json_string as parameters, and then this body can be passed to the .post method of the request builder.
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, json_string);
英文:
After some exploration I found out that we can convert json_string to requestbody json using okhttp Requestbody class create method which takes json mediatype and json_string as parameter and then this body can be passes to .post method of the request builder.
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, json_string);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论