简体   繁体   English

如何在REST中调用put方法

[英]How to call put method in REST assured

I have a put method that accepts inputstream. 我有一个接受inputstream的put方法。 I want to call this method using rest assured in JUnit. 我想在JUnit中使用restured来调用这个方法。

This is what I used: 这是我用过的:

with().body(inpustream).put("/service/1"); // i got error 404 forbidden.

POST will return status code 201 and PUT will return 200, and POST will create a new resource but, PUT will update the existing resource. POST将返回状态代码201,PUT将返回200,POST将创建新资源,但PUT将更新现有资源。 This means we will have to mention which resource we wish to update in the URI itself like below. 这意味着我们将不得不提到我们希望在URI本身中更新哪个资源,如下所示。

import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;
import java.util.HashMap;
import java.util.Map;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.hamcrest.Matchers.*;


public class PUTMethod {

    public static Map<String, String> map = new HashMap<String, String>();

    @BeforeTest
    public void putdata(){
        map.put("userId", "2");
        map.put("id", "19");
        map.put("title", "this is projectdebug.com");
        map.put("body", "i am testing REST api with REST-Assured and sending a PUT request.");  
        RestAssured.baseURI = "http://jsonplaceholder.typicode.com";
        RestAssured.basePath = "/posts/";
    }

    @Test
    public void testPUT(){
        given()
        .contentType("application/json")
        .body(map)
        .when()
        .put("/100")
        .then()
        .statusCode(200)
        .and()
        .body("title", equalTo("this is projectdebug.com"));        
    }
 }

Visit http://www.projectdebug.com/send-put-request-using-rest-assured/ for more information. 有关更多信息,请访问http://www.projectdebug.com/send-put-request-using-rest-assured/

Actually, you are doing well but sending multipart through PUT is unsecured and is quite random ( https://jira.spring.io/browse/SPR-9079 ). 实际上,你做得很好但是通过PUT发送多部分是不安全的并且非常随机( https://jira.spring.io/browse/SPR-9079 )。 Amend your spring-security.xml to add a filter or use POST method in this case. 在这种情况下,修改spring-security.xml以添加过滤器或使用POST方法。

You can also try your code by calling another PUT webservice with no stream. 您也可以通过调用另一个没有流的PUT Web服务来尝试您的代码。

(And what is the error code ? 404 or 403 ?) (什么是错误代码?404或403?)

A similar problem solved by using MultipartFilter : Spring 3.0 FileUpload only with POST? 使用MultipartFilter解决了类似的问题: 只有POST的Spring 3.0 FileUpload?

Have a look at the following example, where it explains how to use PUT request using Rest Assured: 请看下面的示例,其中介绍了如何使用Rest Assured使用PUT请求:

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static com.jayway.restassured.RestAssured.*;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;

public class GetStatusCodeTest {
  @BeforeClass
  public void setBaseUri () {
    RestAssured.baseURI = "https://localhost:3000";
  }

  @Test
  public void updateUsingPut () {
    Posts post = new Posts();
    post.setId ("3");
    post.setTitle ("Hello Bhutan");
    post.setAuthor ("StaffWriter");

    given().body (post)
        .when ()
        .contentType (ContentType.JSON)
        .put ("/posts/3");
  }
}

For detailed explanation, you may check out the following link: https://restservicestesting.blogspot.in/2016/10/automating-put-request-using-rest.html 有关详细说明,您可以查看以下链接: https//restservicestesting.blogspot.in/2016/10/automating-put-request-using-rest.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM