Sending multipart and form params to a SpringMVC controller does not work

783 views
Skip to first unread message

Yacov Schondorf

unread,
Aug 13, 2015, 4:37:50 AM8/13/15
to REST assured
I am trying to send a request that has form and multipart params to a spring MVC controller. The problem is that I am getting an error from the server that a required parameter is not present, although I see in the logging that I am sending it.

The code:
protected Response doMultiPartPostRequest(Map<String,String> headers, List<Cookie> cookies, String uri, String sessionId,
                                         Map<String,String> formParams, Map<String, File> multiPartParams){
Response response = null;
if(formParams == null){
formParams = new HashMap<>();
}
formParams.put("sid", sessionId);

RequestSpecification specification = given();

if (multiPartParams != null) {
for (Entry<String, File> namePathPair : multiPartParams.entrySet()) {
specification = specification.multiPart(namePathPair.getKey(), namePathPair.getValue());
}
}
if (formParams != null) {
specification = specification.formParams(formParams);
}
if (!isDevEnv()) {
specification  = specification.cookies(new Cookies(cookies));
}
response = specification.log().all().sessionId(sessionId).headers(headers).when().post(uri);
response.then().log().all();
return response;
}

Logging + response:


Request method: POST
Request params: <none>
Query params: <none>
Form params: deviceID=28d520aec6f84d2fa771316470c60b73
sid=vzt27whgrsxz13i12fwptfyrv
Path params: <none>
Headers: Content-Type=application/json
...
Cookies: JSESSIONID=vzt27whgrsxz13i12fwptfyrv
Body: <none>
HTTP/1.1 400 Required String parameter 'deviceID' is not present
...
com.jayway.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document
at com.jayway.restassured.path.json.JsonPath$ExceptionCatcher.invoke(JsonPath.java:876)
at com.jayway.restassured.path.json.JsonPath$4.doParseWith(JsonPath.java:837)
at com.jayway.restassured.path.json.JsonPath$JsonParser.parseWith(JsonPath.java:917)
at com.jayway.restassured.path.json.JsonPath.get(JsonPath.java:182)
at com.jayway.restassured.path.json.JsonPath.getBoolean(JsonPath.java:194)
at com.websense.tsaas.portal.ui.devices.DevicesControllerRESTTest.testCertificateUpdate(DevicesControllerRESTTest.java:663)
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:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
Caused by: groovy.json.JsonException: Lexing failed on line: 1, column: 1, while reading '<', no possible valid JSON value or punctuation could be recognized.
at groovy.json.JsonLexer.nextToken(JsonLexer.java:82)
at groovy.json.JsonLexer$nextToken.call(Unknown Source)
at com.jayway.restassured.internal.path.json.ConfigurableJsonSlurper.parse(ConfigurableJsonSlurper.groovy:99)
at com.jayway.restassured.internal.path.json.ConfigurableJsonSlurper$parse.callCurrent(Unknown Source)
at com.jayway.restassured.internal.path.json.ConfigurableJsonSlurper.parseText(ConfigurableJsonSlurper.groovy:85)
at com.jayway.restassured.path.json.JsonPath$4$1.method(JsonPath.java:839)
at com.jayway.restassured.path.json.JsonPath$ExceptionCatcher.invoke(JsonPath.java:874)
... 26 more


Any ideas?

Thanks in advance...

Johan Haleby

unread,
Aug 13, 2015, 10:17:12 AM8/13/15
to rest-a...@googlegroups.com
The log seem to indicate that you only supply form parameters to rest assured in your example (and no multi-parts). Perhaps your service require multipart? Would be easier if you could also show the code of the actual HTTP endpoint. Anyways, form parameters are automatically transformed to multi-part parameters if you have at least one multi-part parameter set. If you don't have any multi-part parameters set the form parameters will not be sent as multi-part form data but as "normal" form data (essentially key values in the request body). So if you want to force multi-part form data you probably should merge the "multiPartParams" and "formParams" maps and use the "multiPart" method for both. 

And a tip. Instead of using the DSL/API (given/when/then syntax) you could create a request spec using the RequestSpecBuilder and the response spec using the ResponseSpecBuilder and then do:

given(requestSpec, responseSpec).when().post(uri);

It's a matter of taste though and you should be fine doing what you're doing today as well.

/Johan


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages