function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jawtechjawtech 

httprequest - large url's

Hi there, I am making a large GET httprequest to an external REST service. The amount of output data to be streamed is about 10000 characters. I can paste the URL in my browser address and I get the response just fine. However using httprequest in apex causes the destination server to say partial URL. Is there no way to stream data in 5000 chunks for example in Apex? Say something similar to this jsp page? the apex httprequest object doesn't seem to have much functions for streaming

 

 

HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod(request.getMethod());
int clength = request.getContentLength();
if (clength > 0) {
    con.setDoInput(true);
    InputStream istream = request.getInputStream();
    OutputStream os = con.getOutputStream();
    final int length = 5000;
    byte[] bytes = new byte[length];
    int bytesRead = 0;
    while ((bytesRead = istream.read(bytes, 0, length)) > 0) {
	os.write(bytes, 0, bytesRead);
    }
}
out.clear();
out = pageContext.pushBody();
OutputStream ostream = response.getOutputStream();

// Return content type
String contentType = con.getContentType();
response.setContentType(contentType);
// Override content types with known issues
if (contentType != null) {
	for (int i = 0; i < contentTypesToMapToXml.length; i++) {
		if (contentType.indexOf(contentTypesToMapToXml[i]) >= 0) {
			response.setContentType("application/xml");
			break;
		}
	}
}
InputStream in = con.getInputStream();
final int length = 5000;
byte[] bytes = new byte[length];
int bytesRead = 0;
while ((bytesRead = in.read(bytes, 0, length)) > 0) {
    ostream.write(bytes, 0, bytesRead);
}

 

 

JWikkJWikk

Upon recommendation of SF 3rd Tier, I have created an enhancement request on Ideas to have SF handle this.

 

https://sites.secure.force.com/ideaexchange/ideaView?id=08730000000IcS1AAK