放心使用POST方法进行授权

时间:2018-08-03 05:15:06

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

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

这是东西:

使用邮递员:POST请求工作正常。

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

在标题下,我给出了X-applicationContent-Type。 我在邮递员的尸体是:

{
  "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尝试时为什么不起作用。

2 个答案:

答案 0 :(得分:0)

添加以下代码即可对其进行修复。从邮递员那里得到了这个令牌

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

答案 1 :(得分:0)

您可以尝试以下方法吗?

  • 创建用于构建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"); 
}