I've built a RESTful service with Spring (java annotation based configuration) which I can execute successfully via Curl. I'm trying to submit files via a HTML Form too, however that's not working.
#RequestMapping(path = "/upload", method = RequestMethod.POST)
public String handleFileUpload(#RequestPart(value = "file") MultipartFile file,
RedirectAttributes redirectAttributes) {
logger.info("POST '/upload'");
storageService.store(file);
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
return "redirect:/";
}
And I'm submitting a file via Curl as follows:
curl -i -H "Content-Type: multipart/*; boundary=------------BOUNDARY--" -X POST --noproxy localhost, localhost:8080/upload -F "file=#test.txt"
Even though my form submission has an input of type file, with the name file, I get this error via a HTML form
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
From what I've been investigating, it would seem as if the controller would expect a Model Attribute when the file is submitted via HTML Form, so what could be a good practice to resolve this? Include a controller parameter for a Model Attribute, which would be checked for null value (to distinguish between html form or other submission methods), or would a sort of Proxy controller between the HTML Form and the RESTful service be better?
The service looks good. It works fine for me on both CURL and HTML form submit.
Here is my form. Please try using this.
<html>
<head>
<title>Post Tool</title>
</head>
<body>
<h1>Sample Requests</h1>
<h2>Upload Document</h2>
<form action="http://localhost:8080/upload" method="post" enctype="multipart/form-data">
Choose a file : <input type="file" name="file" multiple/>
<input type="submit" value="Upload" />
</form>
</body>
</html>
After more research I got to know that the commons multi part resolver doesn't fare well with the new versions of spring boot. It struggles obtaining POST parameters from forms.
One solution is to use the StandardServletMultipartResolver implementation of the MultipartResolver interface!
Related
I feel like I must be missing something obvious because the REST documentation seems so simple and the code I produced appears to work correctly unless I try to access it via REST.
hello.cfc:
component rest="true" restpath="restTest"{
remote string function sayHello() httpMethod="get"{
return "Hello World";
}
}
Service Mapping:
I have tried both default yes and no with no change.
Test Page:
<html>
<head>
<title>REST Test</title>
</head>
<body>
Calling service as an object:<br>
<cfset restTest = new hello() />
<cfdump var="#restTest.sayHello()#" />
<br>
Calling service via http:<br>
<cfhttp url="http://localhost/rest/restTest" result="restResult" method="GET" />
<cfdump var="#restResult#" />
</body>
</html>
Results:
Did you try accessing without directing the call through a connector/webserver?
Keep your project inside /cfusion/wwwroot/ and then try accessing it via browser
http://localhost:8500/rest/restTest
Sample test also worked for me after changing the URL
enter image description here
It seems like you are using a wrong URL when making HTTP request to the REST service. The URL should be like this:
http://{domain}/rest/{service mapping name}/{component level rest path}/{function level restpath}
So in your case the correct URL should be:
http://localhost/rest/api/restTest
For more info read this http://www.adobe.com/devnet/coldfusion/articles/restful-web-services.html
Using XLST 2.0, I have a .html form, and on it an input like
<form method="post" action="perlscript.cgi" name="formname">
.
.
<input name="myvar" onchange="form.submit">
then later, I have
<xsl:if test="myvar=1">
.
.
</xsl:if>
I realize this ISN'T the way to do this because the XSLT doesn’t map the <input name=.. > to the "myvar=1". I think it's probably seeking it from the XML namespace.
I googled XSLT POST , etc, but I don't see examples of this syntax.I immediately get a "Parsing an XSLT stylesheet failed" error with no line number.
If I comment out the <xsl:if> and the </sxl:if>, the form is OK , so I'm sure that's the problem.
So basically I need a way to either merge the XML and FORM namespaces, or a way to access the FORM data.
I am currently creating an engine for a client to submit XML to our web site, and I would like to process the data to a CFC. In order to test this, I have set up a simple form in a regular CFM file, as such...
<!DOCTYPE html>
<html>
<head>
<title>Testing CFC processing</title>
</head>
<body>
<cfsavecontent variable="variables.testxml">
<?xml version="1.0" encoding="UTF-8"?>
<xmlRequest><headers>data</headers><body><Notification><Result Success="1"/><participantID>[ID number]</participantID><transactionNumber>000</transactionNumber></Notification></body></xmlRequest>
</cfsavecontent>
<form method="post" action="[site url]/main.cfc?method=testData">
<cfoutput>
<textarea name="data" cols="150" rows="5">#variables.testxml#</textarea>
</cfoutput>
<br/>
<input type="submit" name="submit" value="Process Test" />
</form>
</body>
</html>
In the main.cfc I have a method called testData which is set up as follows...
remote void function testData(data) {
writeOutput(arguments.data);
}
This test is just to check to see if the method is receiving the data it is sent. When I run it, it does not error, but it also does not output the XML data from the form.
In case it is pertinent, I have the component declared as follows...
component displayname="[name]" accessors="true" output="true"
I can't figure out why the CFC is not receiving the form data (or if it is, why it is not outputting the form data). Can anyone offer any assistance?
I found the answer. It turns out there is no problem with the code above. The problem was in the Application.cfc. I had a method onCFCRequest that didn't have any code in it. It was blocking the CFC from receiving the data. After removing the function from my Application.cfc the method was able to receive the data without issue.
I am creating buttons that send the customer to a hosted payment system. In this case an authorize.net Simple Checkout button in test mode.
<form name="PrePage" method = "post" action = "https://Simplecheckout.authorize.net/payment/CatalogPayment.aspx"> <input type = "hidden" name = "LinkId" value ="8a40541d-2f0f-4bfe-a1e8-397292f5dee5" /> <input type = "image" src ="//content.authorize.net/images/buy-now-gold.gif" /> </form>
My attempt to get the form inputs into the url are following:
https://Simplecheckout.authorize.net/payment/CatalogPayment.aspx/?LinkId=8a40541d%2D2f0f%2D4bfe%2Da1e8%2D3d397292f5dee5
What am I doing wrong?
The diffence is that you are using the GET method instead of POST.
When you include data in your query string (things after the ? in the URL) you are using the GET method.
If you have a form you can specify to use the POST (as it is in your code example also). In this set up the data is transferred a different way. Not in the query string.
see: HTTP - Post and Get
Maybe your server is not handling, only the POST method, so your GET request won't work.
I am tring to retrieve a filename or the file itself for use in a java servlet (from a web form).
I have a file form field:
<form enctype="multipart/form-data" method="post" action="SaveDictionary.do">
<label>
<input type="file" name="dictionary_file" id="dictionary_file" />
<br />
</label>
<label>
<br />
<input type="submit" name="saveDic" id="saveDic" value="Save Dictionary" />
</label>
</form>
I wanto then process it in my servlet, what do I do to process this - for a normal text field I would use something like
String myValue = (String) request.getParameter("parameter_name");
Assuming I have this class, what do I put in the doPost() method to get either the file path or the actual file contents.
#SuppressWarnings("serial")
public class SaveDictionary extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
// I can't work out what goes here, the same
// String myValue = (String) request.getParameter("parameter_name"); doesn't work
response.sendRedirect("dictionary.jsp");
}
}
Multipart/form-data encoded requests are not supported by the default servlet API. You basically need to parse the request body yourself based on the HttpServletRequest#getInputStream(). But that's a precious and tedious work. You don't want to do that if you're already asking this question here at SO. Fortunately there's already a robust, thoroughly developed and maintained API out for that, the Apache Commons FileUpload API. It's in fact easy to use. You can find examples in their User Guide and tips&tricks in their FAQ.
You can also wrap it in a Filter which does all the parsing work and puts all the parameters back in the request, so that it's all transparent in the servlet code and you can continue using HttpServletRequest#getParameter() and consorts the usual way. Uploaded files can then be obtained as request attributes. Here's a basic example.
Hope this helps.