I know this question was asked on stackoverflow before. However, I haven't found an answer that actually tests the service using, for example, Postman.
I have followed the documentation and my service is pretty much the same as described on the documentation:
#PostMapping("/someUrl")
public ResponseEntity<Void> uploadFile(#RequestPart(name="foo", required = false) Foo foo, #RequestPart("file") MultipartFile file) {
return new ResponseEntity<>(OK);
}
When I try to heat my service using Postman, I got the following error:
org.springframework.web.multipart.MultipartException: Current request is not a multipart request
And on postman, I got the following return
The question is: Is it possible to call my service using Postman? If it is, what parameters am I missing?
Yes it is possible to call your service using Postman.
Instead of sending JSON as text, create a file (e.g. test.json) containing JSON data that you want to send:
{
"teste": "abc"
}
Then in form-data section change the type of your foo field from Text to File and choose JSON file that you've created.
And that's it. Click Send and your request should be processed without any error.
Note: Header should be left empty i.e. it should not contain any custom Content-Type value.
Looks like there is some issue with Postman while using multipart file with json data. https://github.com/postmanlabs/postman-app-support/issues/576
You can use curl to test your service like below.
curl -v -H "Content-Type:multipart/form-data" -F "foo={\"teste\":\"abc\"};type=application/json" -F "file=#amazon.png" http://localhost:8080/v1/files/someURL
Related
I am prototyping a very simple POST service to upload a file:
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Path("fileUpload")
public Response uploadFile(MultipartBody body) {
//never gets to here
System.out.println("In uploadFile");
//TODO: implementation
return Response.ok().build();
}
I get
org.apache.cxf.interceptor.Fault: Couldn't determine the boundary from the message!
I also tried to replace the method declaration with:
public Response uploadFile(List<Attachment> attachments,
#Context HttpServletRequest request) {
as per some Google findings, to no help.
I am using two different clients to invoke this service: chrome://poster, which has a field to include a file attachment, and a simple Python script as is explained here. Both clients produce the same result.
How should I change my service implementation or the call or both in order to be able to pass the CXF validation and enter into the actual body of the service?
REFERENCES:
JAX-RS : Support for Multiparts
Apache CXF: JAX-RS Restful web service for uploading/downloading Text file + Java client
The server side code looks fine. Problem is the way you are sending data from client. You are sending data as a stream in payload not as an attachment which has boundary. To verify quickly you can enable logging request and response by enabling CXF Feature LoggingFeature or Interceptors LoggingInInterceptor and LoggingoutInterceptor. In the log if you see data coming Payload then you are sending data as stream in this case you need to change the way you send the data else you can change consumes to application/octetstream and receive data using inputstream directly.
I'm not aware of the tool you are using, however I use Chrome Extension to postman to test the REST services. If you install the extension and launch the application.
You can upload the file using below approach.
Change Method type to POST from the drop down.
Enter the URL
Select Tab Body
Select Form-Data Radio Button
On the right most row select file from drop down. as shown in diagram.
Choose file to upload.
Optional enter multipart key.
Finally click send.
We can reproduce your error by selecting binary radio button and uploading file as shown below.
I would like to post some data to a rest api.
The API documentation (See page 38) asks for the following:
curl -u "USERNAME:PASSWORD" -H "Content-type: text/xml" -X "POST"
--data-binary #-
"https://qualysapi.qualys.com/qps/rest/3.0/create/was/webapp/" <
file.xml
Note: “file.xml” contains the request POST data.
Request POST data:
<ServiceRequest>
<data>
<WebApp>
<name><![CDATA[My Web Application]]></name>
<url><![CDATA[http://mywebapp.com]]></url>
</WebApp>
</data>
</ServiceRequest>
I have confirmed that the call works on the command line using curl.
I then began to write a small app in Java and found UniRest.
Thats where the problem starts. I do not know how to convert the curl request into Unirest.
I have this much so far:
Unirest.post("http://apiurl).basicAuth("user","pass").field(name, file).asBinary();
the latter half
.field(name, file).asBinary();
doesnt make sense to me. What is the intent behind giving the file a name. Isn't suppose to retrieve the data from the file?
Furthermore, I would like to avoid writing data to file. How can I create the same xml with UniRest.
If not xml, could I do the same with JSON? The API attached above (appendix C) also accepts JSON. However, how can I nest fields with the builder pattern of the Unirest api
According to the UniRest documentation, it looks like you can write any array of bytes to a field in the request. You just have to encode the string into a byte array.
Unirest.post("http://apiroot")
.field(name, xmlString.getBytes(StandardCharsets.UTF_8))
.asBinary();
Alternatively, you can use any InputStream,
Unirest.post("http://apiroot")
.field(name, new CharSequenceInputStream(xmlString, StandardCharsets.UTF_8))
.asBinary();
Usually data is the body of the request though (not as a field). If you do want to send the data as the request body and not a form field, you should use the body(String body) method instead of the field(String name, Object object) method, for instance:
String data = "<ServiceRequest>... etc...</ServiceRequest>";
Unirest.post("http://apiroot")
.body(xmlString)
.asBinary();
I am working on a use case where I am displaying user's messages on a JSP. Details of the flow are:
All the messages will be shown in a table with icon for attachments
When the user clicks on attachment, the file should get downloaded.
If there is more than one attachment, user can select the required
one to download.
The attachments will be stored on the local filesystem and the path for the attachments will be determined by the system.
I have tried to implement by referring to these SO questions:
Input and Output binary streams using JERSEY?
Return a file using Java Jersey
file downloading in restful web services
However, it's not solving my purpose. I have the following questions:
Is it possible to send message data (like subject, message, message id, etc) along with the attachments (Inputstream) in one response?
If yes, what needs to be the MediaType for #Produces annotation in my resource method? Currently my resource is annotated with #Produces(MediaType.APPLICATION_JSON). Will this work?
How to send the file data in the response?
Any pointers appreciated. TIA.
You can add custom data to the response Header, so yes you are able to send such message data. Add the data to the response Header.
#Produces(MediaType.APPLICATION_JSON) will not work, unless the clients will accept JSON as a file, what they should and will not do ;)
The correct MediaType depends on what kind of file you want to submit.
You can use the default MediaType / MIME-Type MediaType.APPLICATION_OCTET_STREAM / application/octet-stream (Is there a “default”
MIME type?) but I think it's better to use the correct and exact MIME-Type for your file.
You will find working examples for sending file data with jersey in Input and Output binary streams using JERSEY? - so there is no need to answer this again :)
Hope this was helpful somehow, have a nice day.
I am trying to forward a large file pulled as an input stream to another service using spring's resttemplate. I have followed the answer given by #artbristol in this topic: How to forward large files with RestTemplate?
And it looks like it is setting the body of the request properly (grabbing the request with charlesproxy). The problem is that I have not set the headers correctly since I believe I need to set the content-type as multipart/formdata which I tried by adding this in the callback:
request.getHeaders().setContentType(
new MediaType("multipart", "form-data"));
But in the http headers I am still missing the boundary, not sure how to set that and I am sure I am probably missing some other settings.
So I was able to figure this out. Basically I needed to create a Spring message converter that will take in the input stream and write out to the body. I also basically have to use the Form Message Converter to write out the response body as well.
So in the restTemplate I call an add message converter to add new input stream message converter. In the call back I create a multivaluemap that takes in a string and inputstream and wrap that around an HttpEntity. Then I create a new instance of the Form Message converter and call write, passing in request, and the mutlivaluemap.
It looks like the issue is that I did not include the path to htrace-core.jar in the spark class path:
spark-shell --driver-class-path /opt/cloudera/parcels/CDH/lib/hbase/hbase-server.jar:/opt/cloudera/parcels/CDH/lib/hbase/hbase-protocol.jar:/opt/cloudera/parcels/CDH/lib/hbase/hbase-hadoop2-compat.jar:/opt/cloudera/parcels/CDH/lib/hbase/hbase-client.jar:/opt/cloudera/parcels/CDH/lib/hbase/hbase-common.jar:/opt/cloudera/parcels/CDH/lib/hbase/lib/htrace-core.jar:/etc/hbase/conf
Seems like this is new for spark 1.x
I'm new to Spring, STS, MVC, and web development.
I have a working Spring REST-based web application. I also have a java client which works. I followed these 2 tutorials:
RESTful webservices with Spring
Get started with Spring MVC
What I was able to do is use a java client within the same project and use the RestTemplate postForLocation method. It works! However, now I would like the client to be an iPhone and I don't know how to do that. In the java client, the RestTemplate did a post using the com.project.Transaction class.
Please correct me if I am wrong here. In the XML file, the restTemplate "messageConverter" attribute gets set to jaxbMarshaller which is a Jaxb2Marshaller class with "Transaction" as one of the classes bounded. I don't understand the details of it but I am assuming the RestTemplate takes the Transaction object and marshalls it into XML which gets sent to the server as a POST request.
First question:
Is there any way I can see what the marshalled object (ie: the output) looks like? I'm using STS and please be specific as I am new. For example, maybe the data sent is like <xml ...?
Second question:
I am trying to use curl to make a similar POST request as the java client. This is my curl request:
curl -X POST -H 'Accept:application/xml' -H 'Content-Type: application/xml' http://localhost:8080/BarcodePayment/transactions/ --data "<?xml version="1.0" encoding="UTF-8"?><transaction><amount>3.1</amount><id>5</id><paid>true</id></transaction>"
When I do that, I receive a STATUS 400: syntactically error message.
Third queston:
I would love to be able to understand the details a little bit better. I looked into the source code for RestTemplate from here. In it, the postForLocation method uses HttpEntityRequestCallback which I cannot find any info in Google. I found HttpHeaders in java API doc. However, in RestTemplate, they call getLocation() method which does not exist in the Java API doc under javax.ws.rs.core -> HttpHeaders. How can I understand this stuff better?
I know it's a lot of question. Thanks for the help! Let me know if you need more code snippets and I'll be happy to provide!
Details:
TransactionController
#RequestMapping(method = RequestMethod.POST)
public View addTransaction(#RequestBody Transaction transaction)
{
transactionService.saveTransaction(transaction);
return new RedirectView("/transactionsView/"+ transaction.getId());
}
Your xml is incorrect:
<transaction><amount>3.1</amount><id>5</id><paid>true</id></transaction>
^^^ should be /paid
It turns out I have to use single quotes for the data. After --data "< xml .... ", I change it to ' < xml ... ' and it works.