The request body did not contain the specified number of bytes



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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("01", 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();










share|improve this question
























  • It would be awesome if you could provide a Minimal, Complete, and Verifiable example. Also, the simplest solution is to not set ContentLength (i.e let the framework take care of it for you).

    – mjwills
    Nov 9 '18 at 7:30












  • Is this error coming from the API code? What did you observe in fiddler for the requests which are successful?

    – Chetan Ranpariya
    Nov 9 '18 at 7:30











  • Possible duplicate of How to make HTTP POST web request

    – mjwills
    Nov 9 '18 at 7:33











  • It's not mandatory to provide content length when making a request.

    – Jason Armstrong
    Nov 9 '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 7:29

















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("01", 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();










share|improve this question
























  • It would be awesome if you could provide a Minimal, Complete, and Verifiable example. Also, the simplest solution is to not set ContentLength (i.e let the framework take care of it for you).

    – mjwills
    Nov 9 '18 at 7:30












  • Is this error coming from the API code? What did you observe in fiddler for the requests which are successful?

    – Chetan Ranpariya
    Nov 9 '18 at 7:30











  • Possible duplicate of How to make HTTP POST web request

    – mjwills
    Nov 9 '18 at 7:33











  • It's not mandatory to provide content length when making a request.

    – Jason Armstrong
    Nov 9 '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 7:29













0












0








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("01", 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();










share|improve this question
















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("01", 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();







c# rest






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 5:34







Dilip Nair

















asked Nov 9 '18 at 6:34









Dilip NairDilip Nair

265




265












  • It would be awesome if you could provide a Minimal, Complete, and Verifiable example. Also, the simplest solution is to not set ContentLength (i.e let the framework take care of it for you).

    – mjwills
    Nov 9 '18 at 7:30












  • Is this error coming from the API code? What did you observe in fiddler for the requests which are successful?

    – Chetan Ranpariya
    Nov 9 '18 at 7:30











  • Possible duplicate of How to make HTTP POST web request

    – mjwills
    Nov 9 '18 at 7:33











  • It's not mandatory to provide content length when making a request.

    – Jason Armstrong
    Nov 9 '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 7:29

















  • It would be awesome if you could provide a Minimal, Complete, and Verifiable example. Also, the simplest solution is to not set ContentLength (i.e let the framework take care of it for you).

    – mjwills
    Nov 9 '18 at 7:30












  • Is this error coming from the API code? What did you observe in fiddler for the requests which are successful?

    – Chetan Ranpariya
    Nov 9 '18 at 7:30











  • Possible duplicate of How to make HTTP POST web request

    – mjwills
    Nov 9 '18 at 7:33











  • It's not mandatory to provide content length when making a request.

    – Jason Armstrong
    Nov 9 '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 7:29
















It would be awesome if you could provide a Minimal, Complete, and Verifiable example. Also, the simplest solution is to not set ContentLength (i.e let the framework take care of it for you).

– mjwills
Nov 9 '18 at 7:30






It would be awesome if you could provide a Minimal, Complete, and Verifiable example. Also, the simplest solution is to not set ContentLength (i.e let the framework take care of it for you).

– mjwills
Nov 9 '18 at 7:30














Is this error coming from the API code? What did you observe in fiddler for the requests which are successful?

– Chetan Ranpariya
Nov 9 '18 at 7:30





Is this error coming from the API code? What did you observe in fiddler for the requests which are successful?

– Chetan Ranpariya
Nov 9 '18 at 7:30













Possible duplicate of How to make HTTP POST web request

– mjwills
Nov 9 '18 at 7:33





Possible duplicate of How to make HTTP POST web request

– mjwills
Nov 9 '18 at 7:33













It's not mandatory to provide content length when making a request.

– Jason Armstrong
Nov 9 '18 at 15:07





It's not mandatory to provide content length when making a request.

– Jason Armstrong
Nov 9 '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 7:29





@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 7:29












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer























  • 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 6:14












  • Right, but it seems like something can go into the JSON object strings that makes it too large.

    – slek
    Dec 2 '18 at 6:56











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220912%2fthe-request-body-did-not-contain-the-specified-number-of-bytes%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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.






share|improve this answer























  • 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 6:14












  • Right, but it seems like something can go into the JSON object strings that makes it too large.

    – slek
    Dec 2 '18 at 6:56















0














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.






share|improve this answer























  • 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 6:14












  • Right, but it seems like something can go into the JSON object strings that makes it too large.

    – slek
    Dec 2 '18 at 6:56













0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 5:43









slekslek

396




396












  • 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 6:14












  • Right, but it seems like something can go into the JSON object strings that makes it too large.

    – slek
    Dec 2 '18 at 6:56

















  • 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 6:14












  • Right, but it seems like something can go into the JSON object strings that makes it too large.

    – slek
    Dec 2 '18 at 6:56
















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 6:14






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 6:14














Right, but it seems like something can go into the JSON object strings that makes it too large.

– slek
Dec 2 '18 at 6:56





Right, but it seems like something can go into the JSON object strings that makes it too large.

– slek
Dec 2 '18 at 6:56



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220912%2fthe-request-body-did-not-contain-the-specified-number-of-bytes%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)