无法将cURL POST请求转换为REST Assured请求

时间:2016-03-15 18:30:37

标签: java web-services api rest-assured

我尝试使用REST-Assured来发布此cURL请求:

curl -X POST --data-binary @C:\Leo.jpg "http://<url>/<resource>?apikey={key}&outputMode=json&imagePostMode=raw"

以下是REST-Assured请求:

File file = new File(<file-path>);

InputStream docstream= getClass().getClassLoader().getResourceAsStream("<image-file-name>.jpg");

Response resp = given()
                .header("Content-Length", file.length())
                .contentType(ContentType.BINARY)
                .contentType("multipart/form-data")
                .contentType("multipart/x-www-form-urlencoded")
                .formParam("apikey", apiKey)
                .formParam("outputMode", "json")
                .formParam("imagePostMode", "raw")
                .content(docstream)
                .multiPart(new MultiPartSpecBuilder(docstream)
                        .fileName(file.getName())
                        .controlName("img_file")
                        .mimeType("image/jpeg").build())
                .post("<resource>")
                .then()
                .statusCode(200)
                .extract().response();

cURL请求从服务器返回预期的响应,但REST-Assured请求返回:

org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:886)
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:1827)

尝试过更改标题,multiPart有几种方法没有任何运气。任何帮助非常感谢!谢谢。

1 个答案:

答案 0 :(得分:0)

apiKey,outputMode和imagePostMode参数应该是queryParam而不是formParam