How to increase received JSON data size in Java? - java

I have connected to a REST server using the Java object RestTemplate. The REST responds with the big data, but my program can not receive JSON with length over 10000 chars. Please suggest how to increase the length of JSON received data.

The problem is likely that you are sending the data in a GET request, so it's sent in the URL. Different browsers have different limits for the URL, where IE has the lowest limist of about 2 kB. To be safe, you should never send more data than about a kilobyte in a GET request.
To send that much data, you have to send it in a POST request instead. The browser has no hard limit on the size of a post, but the server has a limit on how large a request can be. IIS for example has a default limit of 4 MB, but it's possible to adjust the limit if you would ever need to send more data than that.

Related

How can I get the number of actual bytes transferred with OkHttp3?

OkHttp does a great job of transparently handling GZIP content encoding. When I call response.body().contentLength() I get the decoded size of the response.
How can I get the number of bytes actually transferred in the HTTP response?
Alternately, getting the value from the original Content-Length header would do.
I am trying to keep track of how many bytes I have downloaded over a metered connection.
Look at EventListener, which tracks bytes transmitted over the network.
https://square.github.io/okhttp/events/

Jetty: Way to get request and response byte size current request processing?

In a Servlet context using Jetty, I would like to know the number of bytes a request was, and the number of bytes the response was (not only the content) - this so that I can log and do stats on this in a Filter upon exiting out.
So far, I've found this:
For response content, I've found that the HttpServletResponse object is a HttpOutput, on which there is a getWritten() returning the number of bytes written - and also, there is a getHttpChannel() returning a HttpChannel, which again has getBytesWritten(). However, both of these only return the size of the content, evidently not including headers - easily seen by a 302 redirect having size 0.
I have also found that from HttpChannel, you can invoke getHttpTransport(), which is a HttpConnection. This has nice "bytesIn" and "bytesOut" LongAdders, which evidently do include all bytes - however, this is for the Connection, and thus with keep-alive, this includes the bytes for all request/responses that this Connection has performed, thus increasing for each request/response cycle that Connection is a part of. (Also, on HttpChannel, there is a getRequests(), which returns the number of requests served with this instance, some kind of average could seemingly be obtained).
Thus: Is there a way to get the total request and response byte sizes for the current request? Bonus for size of content of request too. (I realize that there are two "sizes" to take into account: The one over the wire, which can be compressed, and the actual uncompressed size).

How to receive both binary and JSON in same GET request in SparkJava?

I have a SparkJava application and know how to receive JSON and binary data via separate requests. Is there a way to setup a Route to accept both at the same type? I'm interested in receiving an image and some related text about that image. The goal is to prevent two requests being sent from the client(s).
Theoretically you cannot send 2 types of content per response. I don't know if there is a hack that can simulate 2 types of content per response... If there is none then you have to send two requests from the client. One will respond with the JSON and the other one with the binary data.
As you can see in the following link only one content-type is possible, which can have multiple parameters.
Hope this helps.

URL Size in Google App Engine

I am calling a servlet in my app hosted on GAE. The issue I am having is that my request url is greater than 2048 characters and I am getting 400 Bad Request error. While here it is mentioned that we can make a request with 10MB of data. So how can we send a request with 10MB of data? I am currently using free quota. A similar question was asked long ago but it is not answered yet.
AppEngine limits aside, it doesn't make much sense to put 10MB of data in an URL.
When you take a look at the HTTP protocol, a GET-request looks like this
GET /path/to/resource?possibleParam=value HTTP/1.1
Host: www.example.com
a POST-request like this
POST /path/to/resource?possibleParam=value HTTP/1.1
Host: www.example.com
Content-Type: */*; charset=utf-8
Content-Length: 4242
here come the actual data with a length of 4242 bytes
So if you allow large amounts of data the in the URI of a GET request that would mean that the server doesn't know how much memory it has to allocate in order to receive the whole uri. So to get better performance it does come quite natural that one would restrict the length of GET requests and force you to use POST request instead where the Content-Length must be made known before actually sending bulks of information.
Let's take a look at the comments from other Stackoverflow users
tx802 said:
POST your data?
Alex Martelli, refering to the maximum allowed URL length, said:
it will never be extended to 10 MB -- that obviously calls for a POST
or PUT (where data goes in the body, not the URL!)
That should make sense now, because protocol-wise it doesn't make much sense to push megabytes of data as a URI.
Sending megabytes of data in the request would rather warrant POST or PUT as the request method. This way you can send a request totaling up to 10 megabytes as you've noticed on the referenced article.
The reason you're getting the 400 error is outlined in the urlfetch errors module API documentation; the maximum URL length allowed is 2048 characters.
There is currently an existing feature request for increasing this length; although it's unlikely that this will change in the near future. You can 'star' the issue to get further updates and/or provide your use case in the comments.

how to mark end of data when writing over sockets using java7 async io

I am building a small client server connection where I need to send some payload and get a reply back.
I am using Java 7 Async IO.
My requirement is
to receive the payload
parse the request
do some processing
write the response back
How can I mark the end of request from the client without closing the connection. I can see two options,
Send -1,this implies another read step from server
parse the request bytes as they come to get to know the end, this I want to avoid.
Is there any better way?
Usually you will have either data separator (your option 1), but you have to be ABSOLUTELY sure that this data separator will NEVER be present in your data packet
or
you can prepend the packet with size information (i.e. have an equivalent of packet header)

Categories

Resources