简体   繁体   中英

Multiple POST requests with rest-assured got 500 error on second

I need to send many requests one by one. I have a code:

   public void sendRestRequest(String xmlFile){

        try{
            String myRequest = generateStringFromResource(xmlFile);
            given().auth().basic(prop.getProperty("restLogin"), prop.getProperty("restPassword"))
                    .contentType("application/xml")
                    .body(myRequest.getBytes(StandardCharsets.UTF_8))
                    .when()
                    .post(prop.getProperty("restURL"))
                    .then().
                    assertThat().statusCode(200).and().
                    assertThat().body("status", equalTo("UPLOADED"));
            }
        catch (Exception e){ LOG.error(String.valueOf(e)); }
    }

public static String generateStringFromResource(String path) throws IOException {
        return new String(Files.readAllBytes(Paths.get(path)));
    }

I can successfully create first request. But in the second one I have 500 status code instead of 200. And such error message:

at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:483)
        at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
        at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:655)
        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.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
        at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
        at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:123)
        at io.restassured.specification.ResponseSpecification$statusCode$0.callCurrent(Unknown Source)
        at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:131)
        at io.restassured.internal.ValidatableResponseOptionsImpl.statusCode(ValidatableResponseOptionsImpl.java:119)

May be anybody has ideas? I guess that should be some connection closer or something like this.

On the server side the problem was with xml file, but it is strange, because there are no problems with the same file if I send it first time. After some attempts, I decided to use different approach which works great:

public void sendRestRequest(String xmlFile) throws IOException {
        FileInputStream fis = new FileInputStream("configuration.properties");
        prop.load(fis);
        try {
            URL url = new URL(prop.getProperty("restURL"));
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");
            conn.setRequestProperty("Authorization", prop.getProperty("basic"));

            String input = generateStringFromResource(xmlFile);

            OutputStream os = conn.getOutputStream();
            os.write(input.getBytes());
            os.flush();

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            StringBuilder responseStrBuilder = new StringBuilder();

            String output;
            while ((output = br.readLine()) != null) {responseStrBuilder.append(output);}

            conn.disconnect();

            JSONObject result = new JSONObject(responseStrBuilder.toString());
            Assert.assertEquals(result.getString("status"), "UPLOADED");

        } catch (IOException e) {
            LOG.error(String.valueOf(e));
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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