aspose 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
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: 12825
    
    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: 12825
    
    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: 544
    
    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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Content-Length header not set in REST webservice