0

I am calling an API from by C# Windows service. In some cases the following error is being raised.

The request body did not contain the specified number of bytes. Got 101,379, expected 102,044

In the RAW Request captured using fiddler content length as specified.

Content-Length: 102044

In the response from the API I am receiving the following message.

The request body did not contain the specified number of bytes. Got 101,379, expected 102,044

The strange thing for me is that it does not happen for each and every request, it is generated randomly and different points. Code which I am using to get the content length is specified below.

var data = Encoding.ASCII.GetBytes(requestBody); // requestBody is the JSON String
webReqeust.ContentLength = data.Length;

Is it mandatory to provide content length in REST API calls ?

Edit 1:

This is what my sample code looks like for web request

webReqeust = (HttpWebRequest)WebRequest.Create(string.Format("{0}{1}", requestURI, queryString));
webReqeust.Method = RequestMethod.ToString();
webReqeust.Headers.Add("Authorization", string.Format("{0} {1}", token_type, access_token)); 
webReqeust.Method = RequestMethod.ToString();
webReqeust.ContentType = "application/json";
var data = Encoding.ASCII.GetBytes(requestBody);
webReqeust.ContentLength = data.Length;
using (var streamWriter = new StreamWriter(webReqeust.GetRequestStream()))
{
    streamWriter.Write(requestBody);
    streamWriter.Flush();
    streamWriter.Close();
}
Dilip Nair
  • 61
  • 1
  • 7
  • It would be awesome if you could provide a [mcve]. Also, the simplest solution is to **not set** `ContentLength` (i.e let the framework take care of it for you). – mjwills Nov 09 '18 at 07:30
  • Is this error coming from the API code? What did you observe in fiddler for the requests which are successful? – Chetan Nov 09 '18 at 07:30
  • Possible duplicate of [How to make HTTP POST web request](https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request) – mjwills Nov 09 '18 at 07:33
  • It's not mandatory to provide content length when making a request. – Jason Armstrong Nov 09 '18 at 15:07
  • @JasonArmstrong I am currently doing that and the requests are passing through but how is that the ContentLength obtained in the above method and the one which is actually sent while sending the body are different. Additionally, the difference is not always permanent and random. – Dilip Nair Nov 14 '18 at 07:29
  • It could be a number of things. What does the sending code look like? Is the raw request intact on the server? It could be a matter of encoding in that case. If it's not intact, then it would seem like a network quality issue and there was packet loss. – Jason Armstrong Nov 14 '18 at 10:58
  • @JasonArmstrong I have edited the question to append the sample code. – Dilip Nair Nov 26 '18 at 05:35

1 Answers1

1

I would suggest maybe instead try using HttpClient as done in the linked post from mjwills here. You don't have to use content length, but it sounds like that is being enforced by the API and ultimately you are trying to post too much.

Otherwise the way I see it is that something is making the request body too large. Is it serialized input data which gets encoded into a byte array? If that is what is happening then perhaps the correct length requirements are not being enforced on the data that composes the request body, and I would suggest inspecting what goes on in the composition of the request body object itself.

Kelson Olson
  • 142
  • 2
  • 10
  • Yes, currently I just commented the lines for the content length and it seems to be working fine. But I would like to know why for some requests randomly this issue was getting raised. It is a simple basic JSON string data which goes into the request. The exact expected data to be passed is being obtained in Fiddler when I was trying to check for some unwanted data getting passed through. – Dilip Nair Nov 26 '18 at 06:14
  • Right, but it seems like something can go into the JSON object strings that makes it too large. – Kelson Olson Dec 02 '18 at 06:56