Fix request parameters in curl snippet for multipart requests
Previously, request parameters would be provided via -d. This is incorrect for a multipart request that is also using -F. This commit updates the generation of the curl request snippet to use -F for request parameters when the request is a multipart request. Closes gh-93
This commit is contained in:
@@ -176,6 +176,14 @@ public abstract class CurlDocumentation {
|
||||
this.writer
|
||||
.print(String.format(" -d '%s'", request.getContentAsString()));
|
||||
}
|
||||
else if (request.isMultipartRequest()) {
|
||||
for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
|
||||
for (String value : entry.getValue()) {
|
||||
this.writer.print(String.format(" -F '%s=%s'", entry.getKey(),
|
||||
value));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (request.isPostRequest() || request.isPutRequest()) {
|
||||
String queryString = request.getParameterMapAsQueryString();
|
||||
if (StringUtils.hasText(queryString)) {
|
||||
|
||||
@@ -230,6 +230,15 @@ public class DocumentableHttpServletRequest {
|
||||
return this.delegate.getContextPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map of the request's parameters
|
||||
* @return The map of parameters
|
||||
* @see HttpServletRequest#getParameterMap()
|
||||
*/
|
||||
public Map<String, String[]> getParameterMap() {
|
||||
return this.delegate.getParameterMap();
|
||||
}
|
||||
|
||||
private String getQueryString() {
|
||||
if (this.delegate.getQueryString() != null) {
|
||||
return this.delegate.getQueryString();
|
||||
|
||||
@@ -287,4 +287,19 @@ public class CurlDocumentationTests {
|
||||
result(fileUpload("/upload").file(multipartFile)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartPostWithParameters() throws IOException {
|
||||
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
|
||||
+ "'Content-Type: multipart/form-data' -F "
|
||||
+ "'image=@documents/images/example.png' -F 'a=apple' -F 'a=avocado' "
|
||||
+ "-F 'b=banana'";
|
||||
this.snippet.expectCurlRequest("multipart-post").withContents(
|
||||
codeBlock("bash").content(expectedContent));
|
||||
MockMultipartFile multipartFile = new MockMultipartFile("image",
|
||||
"documents/images/example.png", null, "bytes".getBytes());
|
||||
documentCurlRequest("multipart-post").handle(
|
||||
result(fileUpload("/upload").file(multipartFile)
|
||||
.param("a", "apple", "avocado").param("b", "banana")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user