简体   繁体   English

放心:POST请求示例(帮助)

[英]Rest Assured: POST request example (help)

I want to create POST request with REST Assured . 我想使用REST Assured创建POST请求。 But i can't find a useful guide on the Internet. 但是我在互联网上找不到有用的指南。 Could someone share a good guide (video) or help me to create correct request? 有人可以分享一个好的指南(视频)还是可以帮助我提出正确的请求?

I want to send next request. 我想发送下一个请求。 See request in Fiddler 参见提琴手的要求

I've created next code. 我创建了下一个代码。 See request using Rest Assured 使用“确保放心”查看请求

I'm sure it's wrong. 我确定这是错误的。 So maybe someone will help me ) 所以也许有人会帮助我)

My code: 我的代码:

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();
}

When i'm trying to execute code, next error occurs: 当我尝试执行代码时,发生下一个错误:

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

It quite easy to debug this one, just follow my lead. 调试此程序非常容易,只需遵循我的指导即可。 Firstly you can't override the headers such as Content-Length and Connection without changing the Rest Assured configurations. 首先,您必须先更改诸如“内容长度”和“连接”之类的标题,才能更改“确保余地”的配置。 For better understanding goto : Testing backend timeout with Restassured 为了更好地理解goto: 使用Restassured测试后端超时

Also I made a few modifications to your script which are : 另外,我对您的脚本做了一些修改:

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());
    }

}

It was successful in generating following packet : 成功生成以下数据包:

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=

Hope this helped. 希望这会有所帮助。

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

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