Error while sending attachment to Soap web service - java

On running the example provided in the link https://hc.apache.org/httpcomponents-client-ga/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java, I am getting the below exception
com.sun.xml.ws.server.UnsupportedMediaException: Unsupported Content-Type: multipart/form-data; boundary=APQdLVD1_Zc9bPMSMCmGCk012pvQ6Yv Supported ones are: [text/xml]
Any idea why it is coming?

I think you are confusing two topics:
A HTML form post
A SOAP request post
In the former you indeed send a multipart/form-data as you seem to have done.
In the latter you generally don't send multiparts (unless you are using something like MTOM) but simply post the entire content as "application/soap+xml"
When using MTOM, you don't use multipart/form-data but multipart/related and you still need to follow some conventions that are not found in HTML and as such not in the example code you posted.

Related

HTTP Status 405 - Request method 'POST' not supported in jmeter

I have recorded a script and running it. But I'm receiving the following error.
HTTP Status 405 - Request method 'POST' not supported in jmeter
I'm not sure whether it is script issue or coding issue.
Our application is installed on HTTPS but security certificate not yet installed.
Could any one please help me out.
It could be one of below
The endpoint you are requesting is not supported for POST, It may support only GET, Please change the method in Jmeter to GET or the method your url supports
The endpoint may expect proper CSRF token. It might mismatch with one you recorded. So Please extract CSRF token from previous request's response and use it. You can do via RegEx Extractor in Jmeter.
JMeter does support POST method so it may be due to:
Endpoint you're using doesn't support POST method (it expects other method(s) like GET, PUT, DELETE, etc.)
You're sending not properly configured request.
The most common reason is missing relevant Content-Type header. You can use HTTP Header Manager in order to send the relevant Content-Type header which is usually application/json for REST or text/xml or similar for SOAP.
See Testing SOAP/REST Web Services Using JMeter article for more information on API testing using JMeter.
You can also consider the following approach:
Use specialized Web Services testing tool like SoapUI or RESTClient to fire a request
Either use aforementioned tool or 3rd-party sniffer like Wireshark to capture the request
Configure JMeter to send exactly the same request

Parsing a multi-part response from a http get

I'm developing an integration between 2 applications. Application 1 uses HttpClient GetMethod to request from Application 2. Application 2 will return a multipart response with files embedded. I thought this was a simple exercise, but cannot seem to find common support for parsing a multipart response from HTTP GET. How can Application 1 parse the multipart response from Application 2?
As you are using multi part encode to send the request to the server(Servlet). As multi part encode encrypt all the data in that form you have to decrypt them first and then you can use those values.
Please follow this link.
What does enctype='multipart/form-data' mean?.
Convenient way to parse incoming multipart/form-data parameters in a Servlet.
I was also stuck with same problem. I solved it using javax mail MimeMultiPart. you can see my solution here:- https://stackoverflow.com/a/42548549/5236494
For posterity, there is nothing wrong with this pattern even if it is badly supported by HTTP libraries:
https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
Notice, though that Content-Type isn't multipart/form-data but multipart/mixed but encoding with boundaries between parts is nearly identical.

Set Content-Type as text/plain using EWS java API

I use EWS Java API to send mails through Exchange 2010. From the trace I find that content type is set as text/xml. Is there a way to send mails as plain text only?
Content-Type: text/xml
Thanks.
AFAIK the communication through that API goes with SOAP calls and these should have Content-Type=text/xml; charset=utf-8 - without double quotes around utf-8. EWS is quirky about that.
I think that is what you are seeing in your trace.
The trick is to set the BodyType to Best or HTML or Text whenever you use operations like GetItem, UpdateItem.

If i sent the following response to my browser, am I supposed to get any kind of feedback?

For an assignment, I have written a server that services HTML files and I am supposed to use my web browser as a test client. I am also told that if there is a request for a file that doesn't exist i should send the following
HTTP/1.1 404 Not Found\r\n\r\n and if anything else goes wrong
HTTP/1.1 500 Internal Server Error\r\n\r\n"
I have run tests that should cause those to be sent, but nothing occurs in my browser window? Should I be getting any visual feedback from sending such a request?
In an HTML response there should only be one CRLF (carriage return and line feed) after each line. So you can first remove the extra \r\n.
Also on the second line you can send an HTML response back to the client saying what the error was if you want to show an error. This is normally what a typical web server does where it has its default error page if one is not defined. If you are not sending any HTTP headers, then you can insert the HTML body you want to send back such as <h1> No page found</h1> as the second line.
To include an "entity" in an http response
HTTP/1.1 404 Not Found
Content-Type: text/html
Content-Length: 10
01234576789
each line is ended by CRLF. Be sure to count the Content-Length correctly.
I see that this is a learning exercise, and understand that you may have been told to implement the server this way. But bearing that in mind ...
This is the wrong way to implement a web server / service. The right way is to find an existing implementation and build your service on top of that.
You could use a Java EE web container; i.e. something that implements the Servlets spec.
You could use a non-servlet framework (like Grizzly).
You could build on top of a server-side HTTP protocol stack; e.g. using Apache the HttpComponent library.
Building a web server from the ground up is a lot of work if you are going to do it properly. And the chances that you won't do it properly; i.e. you won't implement your service according how the HTTP spec says a server should behave. You will leave things out, do things the wrong way, etc.
Please don't do it. There are already too many broken (i.e. non-compliant) web servers out there. We don't need more.
And if you DO decide to implement HTTP from the ground up, then you (YOU) need to thoroughly read and understand the HTTP spec. And you (YOU) need to do your own basic research on how browsers implement the client side of the spec ... and what you therefore need to do on the server side to make browsers behave "normally".

How do I use Apache Http Components to relay a POST request from a servlet?

I'm a little unfamiliar both with the Servlet API and Apache Http Components.
I need to handle an incoming POST request with unknown data (although probably the result of a form submission) using HttpServlet.doPost() which I've implemented, and request the same posted information from another URL, effectively acting as a relay for the HTTP POST. I then need to convert the response to a String (it will be text/html) and process it further before returning it to the web browser that requested it from me.
Due to my unfamiliarity with these libraries, its not clear to me how to handle issues like the content-type of the posted data, and also avoiding any problems due to neglecting to release resources.
Can anyone provide any pointers on this?
You should start by having a look at HttpClient class from apache API.
It will handle both get and posts as needed and later you could feel its request with the data you receive in your own servlet.

Categories