Rest Assured:POST请求示例(帮助)

时间:2018-02-19 13:29:22

标签: java post rest-assured

我想使用 REST Assured 创建 POST 请求。但我无法在互联网上找到有用的指南。有人可以分享一个好的指南(视频)或帮我创建正确的请求吗?

我想发送下一个请求。见request in Fiddler

我已经创建了下一个代码。见request using Rest Assured

我确定这是错的。所以也许有人会帮助我)

我的代码:

public static void nextApp(){
    RestAssured.config = RestAssured.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false));
    Response response = given()
            .header("X-Instance-ID", "e2HAWV98vQE:APA91bFU4l4pKKxlpJBDD5IdQXnTMOCrOt7q8muVXW8Qm5uoBvGiBUxjdidq-gVuNtNs9kI5HD3RhDSKUQJAT0UZSNYeYHS2dkewvt974Q0CfgLu02vRaRZj7t75x2Tga4RR3MnxyALC")
            .header("X-Requested-With", "XMLHttpRequest")
            .header("Accept-Language", "en-us")
            .header("Accept-Encoding", "gzip, deflate")
            .header("Content-Type", "application/x-www-form-urlencoded")
            .header("Origin", "https://minsk.nextapp.by")
            .header("X-Device-Token", "AC28F645-ECB6-49A6-AFD6-AE0CD9C1BF58")
            .header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 NextApp/4.3.8")
            .header("Referer", "https://minsk.nextapp.by/?utm_medium=mobile&utm_source=ios&utm_campaign=4.3")
            .header("Connection", "keep-alive")
            .header("Cookie", "io=GybCh81QRgEyoiZpABzk; _ga=GA1.2.977356556.1494829849; _gid=GA1.2.1823073921.1504425333; __cfduid=d53f5a62b6c9edde5756dfe595171c83f1504425330")
            .body("lang=ru&multiplier=&address%5B0%5D%5Bline%5D=%D0%9D%D0%B0%D0%B3%D0%BE%D1%80%D0%BD%D0%B0%D1%8F+%D1%83%D0%BB.+2&address%5B0%5D%5Bcoords%5D=53.9471%2C27.42119&address%5B0%5D%5Bregion%5D=%D0%B0%D0%B3%D1%80%D0%BE%D0%B3%D0%BE%D1%80%D0%BE%D0%B4%D0%BE%D0%BA+%D0%96%D0%B4%D0%B0%D0%BD%D0%BE%D0%B2%D0%B8%D1%87%D0%B8%2C+%D0%96%D0%B4%D0%B0%D0%BD%D0%BE%D0%B2%D0%B8%D1%87%D1%81%D0%BA%D0%B8%D0%B9+%D1%81%D0%B5%D0%BB%D1%8C%D1%81%D0%BE%D0%B2%D0%B5%D1%82%2C+%D0%9C%D0%B8%D0%BD%D1%81%D0%BA%D0%B8%D0%B9+%D1%80%D0%B0%D0%B9%D0%BE%D0%BD%2C+%D0%9C%D0%B8%D0%BD%D1%81%D0%BA%D0%B0%D1%8F+%D0%BE%D0%B1%D0%BB%D0%B0%D1%81%D1%82%D1%8C%2C+%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C&address%5B0%5D%5Bnotes%5D=&address%5B0%5D%5Bstreet%5D=&address%5B1%5D%5Bline%5D=&address%5B1%5D%5Bcoords%5D=&address%5B1%5D%5Bregion%5D=&address%5B1%5D%5Bnotes%5D=&address%5B1%5D%5Bstreet%5D=&tariff=STANDARD&dateOffset=-1&coupon=&phone=%2B375447276177&customer=%D0%A1%D0%B2%D0%B5%D1%82%D0%B0&payment=")
            .baseUri("https://minsk.nextapp.by/order")
            .post();
    System.out.println(response.body().asString());
}

public static void main(String[] args) {
    nextApp();
}

当我尝试执行代码时,会发生下一个错误:

08:59:30.242 [main] DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Get connection for route {s}->https://minsk.nextapp.by:443
08:59:30.296 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnectionOperator - Connecting to minsk.nextapp.by:443
Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2023)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1125)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:543)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:409)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:882)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at org.apache.http.client.HttpClient$execute$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at com.jayway.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder.doRequest(RequestSpecificationImpl.groovy:1974)
at com.jayway.restassured.internal.http.HTTPBuilder.post(HTTPBuilder.java:341)
at com.jayway.restassured.internal.http.HTTPBuilder$post$2.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at com.jayway.restassured.internal.RequestSpecificationImpl.sendRequest(RequestSpecificationImpl.groovy:1159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1210)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:810)
at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:48)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)
at com.jayway.restassured.internal.filter.SendRequestFilter.filter(SendRequestFilter.groovy:30)
at com.jayway.restassured.filter.Filter$filter$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at com.jayway.restassured.filter.Filter$filter.call(Unknown Source)
at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
at com.jayway.restassured.filter.time.TimingFilter.filter(TimingFilter.java:56)
at com.jayway.restassured.filter.Filter$filter.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141)
at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
at com.jayway.restassured.filter.FilterContext$next.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1596)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1210)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:810)
at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:48)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.java:58)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:182)
at com.jayway.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy:164)
at com.jayway.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1210)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:810)
at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:48)
at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.java:58)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
at com.jayway.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy:248)
at com.jayway.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy)
at providers.Providers.nextApp(Providers.java:27)
at providers.Providers.main(Providers.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
08:59:30.359 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection org.apache.http.impl.conn.DefaultClientConnection@1acaf3d closed
08:59:30.359 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection org.apache.http.impl.conn.DefaultClientConnection@1acaf3d shut down
08:59:30.359 [main] DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@1bab8268

1 个答案:

答案 0 :(得分:1)

调试这个很容易,只需按照我的要求。首先,您无法覆盖Content-Length和Connection等标题,而无需更改Rest Assured配置。为了更好地理解转到:Testing backend timeout with Restassured

我还对你的脚本进行了一些修改:

public class Simple1 {

    public static void main(String[] arg) {
        RestAssured.config = RestAssured.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false));
        Response response = given()
                .header("X-Instance-ID", "e2HAWV98vQE:myinstanceID")
                .header("X-Requested-With", "XMLHttpRequest")
                .header("Accept-Language", "en-us")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Content-Type","application/x-www-form-urlencoded")
                .header("Origin", "https://minsk.nextapp.by")
                .header("X-Device-Token", "AC28F645-ECB6-49A6-AFD6-MYDEVICETOKEN")
                .header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 NextApp/4.3.8")
                .header("Referer", "https://minsk.nextapp.by/?utm_medium=mobile&utm_source=ios&utm_campaign=4.3")
                .header("Connection", "keep-alive")
                .header("Cookie", "io=1rtcookie; _ga=2ndcookie; _gid=4rd; __cfduid=5th")
                .body("lang=ru&multiplier=&address%5B0%5D%5Bline%5D=%D0%9D%D0%B0%D0%B3%D0%BE%D1%80%D0%BD%D0%B0%D1%8F+%D1%83%D0%BB.+2&address%5B0%5D%5Bcoords%5D=53.9471%2C27.42119&address%5B0%5D%5Bregion%5D=%D0%B0%D0%B3%D1%80%D0%BE%D0%B3%D0%BE%D1%80%D0%BE%D0%B4%D0%BE%D0%BA+%D0%96%D0%B4%D0%B0%D0%BD%D0%BE%D0%B2%D0%B8%D1%87%D0%B8%2C+%D0%96%D0%B4%D0%B0%D0%BD%D0%BE%D0%B2%D0%B8%D1%87%D1%81%D0%BA%D0%B8%D0%B9+%D1%81%D0%B5%D0%BB%D1%8C%D1%81%D0%BE%D0%B2%D0%B5%D1%82%2C+%D0%9C%D0%B8%D0%BD%D1%81%D0%BA%D0%B8%D0%B9+%D1%80%D0%B0%D0%B9%D0%BE%D0%BD%2C+%D0%9C%D0%B8%D0%BD%D1%81%D0%BA%D0%B0%D1%8F+%D0%BE%D0%B1%D0%BB%D0%B0%D1%81%D1%82%D1%8C%2C+%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C&address%5B0%5D%5Bnotes%5D=&address%5B0%5D%5Bstreet%5D=&address%5B1%5D%5Bline%5D=&address%5B1%5D%5Bcoords%5D=&address%5B1%5D%5Bregion%5D=&address%5B1%5D%5Bnotes%5D=&address%5B1%5D%5Bstreet%5D=&tariff=STANDARD&dateOffset=-1&coupon=&phone=%2B375447276177&customer=%D0%A1%D0%B2%D0%B5%D1%82%D0%B0&payment=")
                .baseUri("https://minsk.nextapp.by/order")
                .post();
        System.out.println(response.body().asString());
    }

}

成功生成了以下数据包:

POST /order HTTP/1.1
Origin: https://minsk.nextapp.by
Cookie: io=1rtcookie; _ga=2ndcookie; _gid=4rd; __cfduid=5th
X-Requested-With: XMLHttpRequest
X-Device-Token: AC28F645-ECB6-49A6-AFD6-MYDEVICETOKEN
Accept: */*
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 NextApp/4.3.8
Referer: https://minsk.nextapp.by/?utm_medium=mobile&utm_source=ios&utm_campaign=4.3
Connection: close
Accept-Language: en-us
Accept-Encoding: gzip, deflate
X-Instance-ID: e2HAWV98vQE:myinstanceID
Content-Type: application/x-www-form-urlencoded
Content-Length: 954
Host: minsk.nextapp.by

lang=ru&multiplier=&address%5B0%5D%5Bline%5D=%D0%9D%D0%B0%D0%B3%D0%BE%D1%80%D0%BD%D0%B0%D1%8F+%D1%83%D0%BB.+2&address%5B0%5D%5Bcoords%5D=53.9471%2C27.42119&address%5B0%5D%5Bregion%5D=%D0%B0%D0%B3%D1%80%D0%BE%D0%B3%D0%BE%D1%80%D0%BE%D0%B4%D0%BE%D0%BA+%D0%96%D0%B4%D0%B0%D0%BD%D0%BE%D0%B2%D0%B8%D1%87%D0%B8%2C+%D0%96%D0%B4%D0%B0%D0%BD%D0%BE%D0%B2%D0%B8%D1%87%D1%81%D0%BA%D0%B8%D0%B9+%D1%81%D0%B5%D0%BB%D1%8C%D1%81%D0%BE%D0%B2%D0%B5%D1%82%2C+%D0%9C%D0%B8%D0%BD%D1%81%D0%BA%D0%B8%D0%B9+%D1%80%D0%B0%D0%B9%D0%BE%D0%BD%2C+%D0%9C%D0%B8%D0%BD%D1%81%D0%BA%D0%B0%D1%8F+%D0%BE%D0%B1%D0%BB%D0%B0%D1%81%D1%82%D1%8C%2C+%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C&address%5B0%5D%5Bnotes%5D=&address%5B0%5D%5Bstreet%5D=&address%5B1%5D%5Bline%5D=&address%5B1%5D%5Bcoords%5D=&address%5B1%5D%5Bregion%5D=&address%5B1%5D%5Bnotes%5D=&address%5B1%5D%5Bstreet%5D=&tariff=STANDARD&dateOffset=-1&coupon=&phone=%2B375447276177&customer=%D0%A1%D0%B2%D0%B5%D1%82%D0%B0&payment=

希望这会有所帮助。