【问题标题】:Authorization in POST method using rest assuredPOST方法中的授权使用放心
【发布时间】:2018-08-03 05:15:06
【问题描述】:

我正在尝试向我的 API 发送一个 POST 方法,它返回 401 错误而不是 200 OK。

事情是这样的:

使用 Postman:POST 请求工作正常。

在授权部分,我选择了Basic Auth 并提供了用户名/密码。

在标题部分,我给出了X-applicationContent-Type。 我在 Postman 的身体是:

{
  "description": "TestAPIPostmannolast",
  "issue_type": {
    "id": "271341877549072389",
    "name": "Design"
  },
  "area": {
    "id": "271341877549072406"
  },
  "issue_id": "9d5ac7da-9626-11e8-9eb6-529269fb1459"
}

使用 RestAssured:失败并出现 401 错误。

package basic;

import io.restassured.response.Response;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import static io.restassured.RestAssured.given;

public class GetRequest {


    @BeforeClass
    public void setup() {

        RestAssured.baseURI = "https://123.com";

        RestAssured.basePath = "/field-management/api";

        RestAssured.authentication =    RestAssured.basic("username", "password");

    }

    @Test
    public void responseBody(){

        Response response = given()
                .header("X-application", "63fc4887-aed9-497f-bad5-d7ef2b90cdaf")
                .contentType("application/json")
                .body("{\n" +
                        "  \"description\": \"Intellij  \",\n" +
                        "  \"issue_type\": {\n" +
                        "    \"id\": \"271341877549072389\",\n" +
                        "    \"name\": \"Design\"\n" +
                        "  },\n" +
                        "  \"area\": {\n" +
                        "    \"id\": \"271341877549072406\"\n" +
                        "  },\n" +
                        "  \"issue_id\": \"2dd8ae7a-966e-11e8-9eb6-529269fb1459\n" +
                        "\n\"\n" +
                        "}")

                .when()
                .post("/projects/1879048400/areas/271341877549072406/issue/");

        System.out.println(response.body().asString());
    }

回复:

{"message":"Authentication Failed","errorKey":null}

不知道为什么当我通过 RestAssured 尝试它时它不起作用。

【问题讨论】:

    标签: rest postman basic-authentication rest-assured web-api-testing


    【解决方案1】:

    添加下面的代码修复了它。从邮递员那里得到这个令牌

     .header("Authorization","Basic cG9sZWFyeTpBdXRoM250MWM=")
    

    【讨论】:

      【解决方案2】:

      你可以试试下面的方法吗:

      • 创建用于构建 RequestSpecification 的 Utility 方法
      • 使用构建的 RequestSpecification 对象进行 POST 调用。您可以在 RequestSpecification 中嵌入身份验证部分。

        private static RequestSpecification request_spec_builder_POST(Object _body, String _username, String _pass){
            RequestSpecBuilder _builder = new RequestSpecBuilder();
            _builder.setBody(_body);
        
            _builder.setContentType("application/json");
            PreemptiveBasicAuthScheme auth = new PreemptiveBasicAuthScheme();
            auth.setUserName(_username);
            auth.setPassword(_pass);
            _builder.setAuth(auth);
            RequestSpecification _spec = _builder.build();
            return RestAssured.given(_spec);
        }
        

      在您的测试类中使用它来进行 POST 调用,如下所示:

      @Test
      public void myTest(){
         RequestSpecification http_req = MyRequestSpecBuilder.request_spec_builder_POST(_obj, username, pass);
         Response _response = http_req.post("/endPoint"); 
      }
      

      【讨论】:

        猜你喜欢
        • 2014-02-19
        • 2018-02-19
        • 1970-01-01
        • 2011-04-05
        • 2019-05-05
        • 2017-12-13
        • 1970-01-01
        • 2021-11-16
        • 2020-01-29
        相关资源
        最近更新 更多