rest - 放心的POST请求

标签 rest rest-assured

我对包含JSON正文的发布请求使用放心

我的职位要求代码是:-

RestAssuredResponseImpl stat=
            (RestAssuredResponseImpl)given().
            header("Accept", "application/json").
            header("Content-Type", "application/json").
            header("userid", "131987”).
            queryParam("name", "Test12").
            queryParam("title", "Test127123").
            queryParam("contactEmail", “abc@gmail.com").
            queryParam("description", "testing purpose").
            when().post("").thenReturn().getBody();

我收到以下错误:
{"errors":{"error":{"code":400,"type":"HttpMessageNotReadableException","message":"Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@8e9299c"}}}

请帮助。

最佳答案

看起来您的服务器正在等待请求正文,但您正在将数据作为查询参数发送。如果我理解正确,则希望将数据作为JSON发送。最简单的方法是使用this方法:

Map<String, Object>  jsonAsMap = new HashMap<>();
map.put("name", "Test12");
map.put("title", "Test127123");
map.put("contactEmail", “abc@gmail.com");
map.put("description", "testing purpose");

ResponseBody = 
given().
        accept(ContentType.JSON).
        contentType(ContentType.JSON).
        header("userid", "131987”).
        body(jsonAsMap).
when().
        post("").
thenReturn().body();

关于rest - 放心的POST请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31875534/

相关文章:

java - 在 Rest-assured 中使用 Json 文件作为负载

java - 放心,JSON : verify absence of a value in a field

java - 验证响应中特定 JSON 对象的 json 模式

Restassured 测试错误 - No Content-Type was specified in response

rest - 用于运行分面搜索的 Alfresco API

rest - 如果客户端请求由于某些业务逻辑而无法完成,服务器应该返回什么 HTTP 状态代码?

c# - DataContract 不使用属性中指定的名称属性

java - 确定此 REST 应用程序背后的工具?

java - 放心代码不允许使用 println

java - 确保 cookie 或授权 header 保持最新