【问题标题】:How can i catch referer value dynamically changing in request header我如何捕获请求标头中动态变化的引用值
【发布时间】:2020-10-03 13:45:33
【问题描述】:

我想将引用值添加到我的代码中,但是当我通过 Chrome 的检查从网络选项卡检查它时,引用值不是恒定的,它总是在变化。我如何在我的代码中捕获和定义它?有没有办法将引用者设置为请求标头中的参数?

我正在通过 Java 使用 RestAssure 框架。

   RestAssured.baseURI="https://test-v2-api.mykredit.com/api/customer/light";
    //RestAssured.baseURI="https://test-v2-api.mykredit.com/api/membership";
    RequestSpecification httpRequest2 = RestAssured.given();
    JSONObject requestparam2 = new JSONObject();
    System.out.println(mobilenum.substring(5)); 
    requestparam2.put("pin",mobilenum.substring(5));
    
    httpRequest2.header("Content-Type","application/json; charset=utf-8");
    httpRequest2.body(requestparam2.toJSONString());
    
    //Response response = httpRequest2.request(Method.POST,"/login");
    Response response = httpRequest2.request(Method.POST,"/pinValidate");
    String responseBody = response.getBody().asString();
    System.out.println(responseBody);
    
    int status = response.getStatusCode();
    System.out.println("Status"+" "+status);
    Assert.assertEquals(status, 200);
    
}

【问题讨论】:

    标签: java json rest rest-assured rest-assured-jsonpath


    【解决方案1】:

    您可以使用io.restassured.response (java documentation) 中的getHeader(String name) 方法从响应标头中检索值。

    Response response = httpRequest2.request(Method.POST,"/pinValidate");
    String referer = response.getHeader("referer");
    

    进口;

    import io.restassured.response.Response;
    

    您也可以使用header(String name) 方法。


    更新

    您可以使用io.restassured.internal.RequestSpecificationImpl (Java documentation) 中的getHeaders() 方法来检索请求标头。

    Response response = httpRequest2.request(Method.POST,"/pinValidate");
    String refererValue = ((RequestSpecificationImpl) httpRequest2).getHeaders().get("referer").getValue();
    

    进口;

    import io.restassured.internal.RequestSpecificationImpl;
    

    【讨论】:

    • 感谢您的回答,但我没有明白您的意思。 referer 值位于请求标头中而不是响应标头中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    相关资源
    最近更新 更多