File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes Content-Length header not set in REST webservice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Content-Length header not set in REST webservice" Watch "Content-Length header not set in REST webservice" New topic
Author

Content-Length header not set in REST webservice

Adyan Faruqi
Greenhorn

Joined: Mar 22, 2013
Posts: 3
Hi,

I am using Junit for testing a REST webservice using Jersey API. I want to send the Content-Length header as my target host needs it.

If I try to specify content-length (request.header("Content-Length","475")), I get the following error,

Exception is: org.apache.http.client.ClientProtocolException
Caused by: org.apache.http.ProtocolException: Content-Length header already present


I would like to understand that if content-Length is already present implicitly, then why is the target-host not receiving it. How do I send the info in the request. When I do not specify the header, I get the following error from the host.

ApacheHttpClient4 client = HttpUtils.getClient();
WebResource wr = client.resource("https://myhost/myresourceendpoint")
ClientResponse cr = wr.header("Authorization","absxbsnsnsnsazd");
.header("Content-Type","text/plain");
.post(ClientResponse.class,f) //f is the file containing the text

1 > POST https://myhost/myresourceendpoint
1 > Authorization: absxbsnsnsnsazd
1 > Content-Type: text/plain
Some text in body


400
{
"Error": "Content-Length not present"
}


Thanks,
Adyan
Adyan Faruqi
Greenhorn

Joined: Mar 22, 2013
Posts: 3
This has finally got resolved by converting the FileInputStream into array of bytes.

File file = null;

audiofilelocation = "C:/myfile.txt";

file = new File(URLDecoder.decode(getClass().getClassLoader()
.getResource(audiofilelocation).getFile(), "UTF-8"));

FileInputStream f = new FileInputStream(file);
ByteArrayOutputStream os = new ByteArrayOutputStream();
do {
os.write(f.read());
} while (f.available() > 0);

byte[] b = os.toByteArray();

cr = wb.post(ClientResponse.class,b);

This way the Content-Length header info is sent in the request
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
I don't think the available method is what you want. See the java.io.FileInputStream JavaDocs.

I believe it can return 0 before the end of the file is reached.

The read() method will return -1 if the eof is hit so your os.write is going to see -1 - will write 0xFF to your output.

Bill
Adyan Faruqi
Greenhorn

Joined: Mar 22, 2013
Posts: 3
It has worked for me. I am getting the correct Content-Length at target. Also the attachment I am giving is a text file.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Using available() may work correctly 1023 times, but if the operating system code reading the file happens to not have a buffer load and your last read exactly used the last available character, it will fail. See the JavaDocs.

Bill

g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
I think the buffering does take place and that's why Content-Length can thereby be made available through the byte[]. It seems that is a somewhat faq for Jersey, I suppose, and that if we can hear what each other is trying to say...
http://jersey.576304.n2.nabble.com/Not-chunked-td2696707.html
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Content-Length header not set in REST webservice
 
Similar Threads
Error while genrating a simple response
"org.xml.sax.SAXParseException: Premature end of file" Problem
PC Connected to Internet and Internet Connectivity?
Problem with Axis2 via proxy with NTLM authentication (currently defaulting to BASIC)
how to bind authorization code with http request when sending webservice request