Monitoring HTTP requests and responses (no web browser) - web-services

I'm creating web services to make an Android app talk to a server.
I'm using Tomcat for the server and a JAX RS lib to convert data between Java objects and JSON during the transfers.
What I want is to be able to view the actual HTTP requests and responses the framework is creating.
A web browser (like Firefox with Firebug) won't do because I also need to check the request bodies generated by the Android app.
With the Servlet API I could intercept and get the request data with a filter, but not the response (I believe) after it was written by the framework.
Wireshark might be a solution (I'm trying it right now), but seems to be a bit to much. I need to worry about properly filtering my messages and didn't figure out yet how to get my HTTP messages properly formatted (plain text w/o the hexa content).
I wonder if there is a simpler way to do this from inside my application or from tomcat.

Try Tomcat's AccessLogValve. You can configure it to log incoming request headers as well as outgoing response headers. Here's an example:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_headers_access_log." suffix=".txt"
pattern="%t "%r" %s ||| %{Content-Length}o ||| %{User-Agent}i" />
Notice the %{xxx}o pattern - that's for response header. You might want to use it as %{X-JSON}o. Also notice the %{xxx}i pattern - that's for request headers.
Nest the <Valve/> element within a <Context/>, <Host/> or <Engine/> elements and you'll be all set.

I ended up using Wireshark with the following filter:
http.content_type == "application/json" || http.accept ==
"application/json"
To get the content decoded, right click on the json part of you package ("JavaScript Object Notation: application/json") > Copy > Bytes > Printable text only

Related

Can I see the responses to requests when capturing requests with Postman?

I have the proxy supported by Postman running. I'm using this to capture requests from integration tests.
I can see the requests in the history tab, but request details do not include response data, such as http code, response body etc.
If I click send, i.e. repeat the request with Postman, then this data is displayed.
I'm really confused here, since Postman is the proxy and is able to capture the request and response both, why on earth it won't bother displaying the response data, and only display details of the request?
How do I display the response to captured requests from another process??
To provide further details: I'm using the proxy, not the interceptor because I'm making calls to localhost and interceptor won't capture those.
Here is what the UI looks like, with the giant question mark showing the empty response section :)
The entries in the history tab are generated by integration tests making calls to a development server running on localhost.
If I repeat the particular (currently selected) request by clicking the send button, it'll display the json response happily.
Postman is the latest version (as of the date and time of this post) running under WSL2 with Ubuntu 20.04
Since version 8.9, Postman now allows you to capture responses along with requests.

Handling soap+msbin1 content type in Jmeter

I am trying to use Jmeter for performance testing of a WPF app that uses WCF Web Service. I see that the service is using msbin1 encoding format. Hence i am no able to make out what the request parameters are. How to handle this in Jmeter
In order to make the request you need to just add HTTP Header Manager and configure it to send Content-Type header with the value of soap-msbin1. In order to send the actual payload you can use i.e. HTTP Raw Request sampler.
If you need extended functionality, i.e. to be able to build requests from text and perform correlation of responses you will have to use the code from WCF-Binary-SOAP-Plug-In in JSR223 Test Elements and perform payload encoding/decoding using Groovy language

The procedure of Opening a website using IE8

I want to know when I'm using IE8 open a website (like www.yahoo.com), which API will be called by IE8? so I can hook these API to capture which website that IE8 opening currently.
When you enter a URL into the browser, the browser (usually) makes an HTTP request to the server identified by the URL. To make the request, the IP address of the server is required, which is obtained by a DNS lookup of the host (domain) name.
Once the response -- usually containing HTML markup -- is received, the browser renders it to display the webpage.
More details available here: what happens when you type in a URL in browser
So, in the general case, no "API" request as such is made. (Technically speaking, you can think of the original HTTP request to the server as an API request). The sort of "API" request you presumably mean, however, is not made in this general case just described. Those requests happens when the JavaScript executing on the page makes an Ajax HTTP request (XmlHttpRequest) to the web server to carry out some operation.
I am not sure about IE8, but the "developer tools" feature of most modern browsers (including IE9 and IE10), would let you see the Ajax HTTP requests that the webpage made as it carried out different operations.
Hope this helps.
IE uses Microsoft's WinSock library API to interact with web servers.
You may want to look for a network monitoring/sniffing API, which you could use to examine HTTP requests, and determine the URLs the browser is using.

Consume REST service that returns a single value

I am used to consuming Web services via a XMLHttpRequest, to retrieve xml or JSON.
Recently, I have been working with SharePoint REST services, which can return a single value (for example 5532, or "Jeff"). I am wondering if there is a more efficient way than XMLHttpRequest to retrieve this single value. For example, would it work if I loaded the REST url via an iframe, then retrieved the iframe content? Or is there any other well established method?
[Edit] By single value, I really mean that the service just returns these characters. This is not even presented in a JSON or xml response.
Any inefficiency in XMLHttpRequest is largely due to the overhead of HTTP, which the iframe approach is going to incur, as well. Furthermore, if the Sharepoint service expects to speak HTTP, you're going to need to speak HTTP. However, an API does not have to run over HTTP to be RESTful, per Roy Fielding, so if the service provided an API over a raw socket -- or if you simply wanted to craft your own slimmer HTTP request -- you could use a Flash socket via a library like: http://code.google.com/p/javascript-as3-socket/. You could cut the request message size down to under 100 bytes, and could pull out the response data trivially.
The jQuery library is a well established framework which you can use. It´s also an article which answer your concrete question at StackOverflow.

Setting HTTP headers through Axis2 API

I am using apache axis2 server webservies, Basically I am sending xml response to android client through webservices. Here I need to maintain the session since the services per user basis. I know maintaining session in webservices is bad idea, but cant avoid it.
Actually I need to generate random unique string when user invoke first service from android client, that random string going to be used as session id. This session id, i need to set in http custom header, so that android client can able to get it and can send it subsequent requests as well.
I want to know whether any API is available in axis2 to set custom header information on http headers. Same way I need to read the http header, so that next request I can get the session id from header.
Can anyone advice me regarding this?? Thanks
-Ravi
Dead link on #Martin Dürrmeier's answer, here's a snapshot of the webpage that i've found on web.archive.org : Axis2 - Setting custom HTTP Headers on a response, it helped me.
Here's the lines needed :
MessageContext responseMessageContext =
MessageContext.getCurrentMessageContext().getOperationContext().getMessageContext(
WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
List<Header> headers = new ArrayList<Header>();
headers.add(new Header(HTTPConstants.HEADER_CONTENT_ENCODING, "identity"));
responseMessageContext.setProperty(HTTPConstants.HTTP_HEADERS, headers);