api - 如何在 Rest Assured Framework 中使用 POST 方法
问题描述
如何在放心框架中使用 post 方法。我正在通过使用 Rest Assured Framework 来学习 API,任何人都可以帮助我如何构建
我的代码:
RestAssured.baseURI ="https://reqres.in";
RequestSpecification request = RestAssured.given();
JSONObject requestParams = new JSONObject();
requestParams.put("email", "sydney@fife");
requestParams.put("password", "pistol444");
request.body(requestParams.toJSONString());
Response response = request.post("/api/register");
int statusCode = response.getStatusCode();
System.out.println("API response code is :" + statusCode);
String body =response.asString();
System.out.println("Response body is : "+ body);
使用此 Post 方法后收到错误消息:
API 响应代码为:400 响应正文为:{"error":"Missing email or username"}
这是参考 URL:https ://reqres.in/ 并且发布方法是:REGISTER - SUCCESSFUL
请帮我 。
谢谢
解决方案
您还没有添加标题,下面代码中的第三行
RestAssured.baseURI = "https://reqres.in";
RequestSpecification request = RestAssured.given();
request.header("Content-Type","application/json");
JSONObject requestParams = new JSONObject();
推荐阅读
- mozilla-deepspeech - 在 Google colab 上训练 deepspeech 需要遵循哪些步骤?
- android - 如何将 RowSupportFragment 项目对齐到中心,就像倾斜中的 VerticalGridSupportFragment
- linux - 为什么 lsb_release 命令在 ubuntu 上不起作用?
- sql - 触发器将数据从视图复制到表 - T-SQL
- reactjs - 如何在 Firestore 查询中应用逻辑 AND (&&)
- spring - 带有 Kotlin 的 Youtube 数据 API:请求缺少有效的 API 密钥
- sql - Bigquery 计算从月初到月底/当前日期的拆分
- css - 通过 CSS 选择性地调整图像大小
- mysql - 带有 Symfony 进程的 mysql 命令
- vue.js - Vue 在路由器文件中访问 Vuex 存储