Core 2.1 HttpRequest.Body is empty when trying to read it
I am trying to read stream data from HttpRequest.Body but I am getting empty string.
The request is send here from .net project
HttpWebRequest request = null;
Uri uri = new Uri(**Endpoint**);
UTF8Encoding encoding = new UTF8Encoding();
byte bytes = encoding.GetBytes(message);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.UseDefaultCredentials = true;
using (Stream writeStream = request.GetRequestStream()) {
writeStream.Write(bytes, 0, bytes.Length);
}
try {
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
return true;
} else {
return false;
}
} catch {
lock (endpointLock) {
_pushHttpEndpoint = null;
}
return false;
}
The request is send here.This is .net core 2.1 application. I am trying to read the data in request body but that is returning empty
[HttpPost]
public string Post()
{
var bodyStr = "";
var req = HttpContext.Request;
req.EnableRewind();
using (StreamReader reader
= new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
{
bodyStr = reader.ReadToEnd();
}
req.Body.Seek(0, SeekOrigin.Begin);
//do other stuff
return bodyStr;
}
Can someone please help me with this.
We are in position where we cannot change the .net solution code. Any changes should be done in .net core solution side. We are trying to fit the new api in place of existing Endpoint. :(
asp.net-core asp.net-core-mvc httpwebrequest asp.net-core-webapi asp.net-core-2.1
add a comment |
I am trying to read stream data from HttpRequest.Body but I am getting empty string.
The request is send here from .net project
HttpWebRequest request = null;
Uri uri = new Uri(**Endpoint**);
UTF8Encoding encoding = new UTF8Encoding();
byte bytes = encoding.GetBytes(message);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.UseDefaultCredentials = true;
using (Stream writeStream = request.GetRequestStream()) {
writeStream.Write(bytes, 0, bytes.Length);
}
try {
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
return true;
} else {
return false;
}
} catch {
lock (endpointLock) {
_pushHttpEndpoint = null;
}
return false;
}
The request is send here.This is .net core 2.1 application. I am trying to read the data in request body but that is returning empty
[HttpPost]
public string Post()
{
var bodyStr = "";
var req = HttpContext.Request;
req.EnableRewind();
using (StreamReader reader
= new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
{
bodyStr = reader.ReadToEnd();
}
req.Body.Seek(0, SeekOrigin.Begin);
//do other stuff
return bodyStr;
}
Can someone please help me with this.
We are in position where we cannot change the .net solution code. Any changes should be done in .net core solution side. We are trying to fit the new api in place of existing Endpoint. :(
asp.net-core asp.net-core-mvc httpwebrequest asp.net-core-webapi asp.net-core-2.1
add a comment |
I am trying to read stream data from HttpRequest.Body but I am getting empty string.
The request is send here from .net project
HttpWebRequest request = null;
Uri uri = new Uri(**Endpoint**);
UTF8Encoding encoding = new UTF8Encoding();
byte bytes = encoding.GetBytes(message);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.UseDefaultCredentials = true;
using (Stream writeStream = request.GetRequestStream()) {
writeStream.Write(bytes, 0, bytes.Length);
}
try {
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
return true;
} else {
return false;
}
} catch {
lock (endpointLock) {
_pushHttpEndpoint = null;
}
return false;
}
The request is send here.This is .net core 2.1 application. I am trying to read the data in request body but that is returning empty
[HttpPost]
public string Post()
{
var bodyStr = "";
var req = HttpContext.Request;
req.EnableRewind();
using (StreamReader reader
= new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
{
bodyStr = reader.ReadToEnd();
}
req.Body.Seek(0, SeekOrigin.Begin);
//do other stuff
return bodyStr;
}
Can someone please help me with this.
We are in position where we cannot change the .net solution code. Any changes should be done in .net core solution side. We are trying to fit the new api in place of existing Endpoint. :(
asp.net-core asp.net-core-mvc httpwebrequest asp.net-core-webapi asp.net-core-2.1
I am trying to read stream data from HttpRequest.Body but I am getting empty string.
The request is send here from .net project
HttpWebRequest request = null;
Uri uri = new Uri(**Endpoint**);
UTF8Encoding encoding = new UTF8Encoding();
byte bytes = encoding.GetBytes(message);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.UseDefaultCredentials = true;
using (Stream writeStream = request.GetRequestStream()) {
writeStream.Write(bytes, 0, bytes.Length);
}
try {
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
return true;
} else {
return false;
}
} catch {
lock (endpointLock) {
_pushHttpEndpoint = null;
}
return false;
}
The request is send here.This is .net core 2.1 application. I am trying to read the data in request body but that is returning empty
[HttpPost]
public string Post()
{
var bodyStr = "";
var req = HttpContext.Request;
req.EnableRewind();
using (StreamReader reader
= new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
{
bodyStr = reader.ReadToEnd();
}
req.Body.Seek(0, SeekOrigin.Begin);
//do other stuff
return bodyStr;
}
Can someone please help me with this.
We are in position where we cannot change the .net solution code. Any changes should be done in .net core solution side. We are trying to fit the new api in place of existing Endpoint. :(
asp.net-core asp.net-core-mvc httpwebrequest asp.net-core-webapi asp.net-core-2.1
asp.net-core asp.net-core-mvc httpwebrequest asp.net-core-webapi asp.net-core-2.1
edited Nov 26 at 11:46
asked Nov 20 at 7:09
Amrutha
213
213
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Your type is "type is application/x-www-form-urlencoded" , so use [FromForm] attribute . Code below is for your reference :
.Net project :
HttpWebRequest request = null;
Uri uri = new Uri("https://localhost:44365/Home/Post");
UTF8Encoding encoding = new UTF8Encoding();
var postData = "thing1=hello";
postData += "&thing2=world";
byte bytes = encoding.GetBytes(postData);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.UseDefaultCredentials = true;
using (Stream writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
//return true;
}
else
{
// return false;
}
}
catch(Exception e)
{
}
On your .net Core Project :
[HttpPost]
public string Post([FromForm]AcceptValue acceptValue)
{
//do other stuff
return "";
}
public class AcceptValue {
public string thing1 { get; set; }
public string thing2 { get; set; }
}
Actually we are trying to hack our code. The problem is that the data cannot be sent in this way. var postData = "thing1=hello"; postData += "&thing2=world"; .Net code is the old one and we are in situation not to change that. Data that is sent can be any format and we need that in .net core solution. Any changes should be done in .net core solution side. We are in tricky position. :(
– Amrutha
Nov 23 at 6:39
add a comment |
You should use the [FromBody] attribute.
It will be this:
[HttpPost]
public string Post([FromBody] object body) {
// do stuff here
}
1
This doesnt help. [FromBody] for "application/x-www-form-urlencoded" request gives 415 unsupported media type error in .net core.
– Amrutha
Nov 21 at 16:47
@Amrutha , FromBody is used for json type , you should use FromForm if the type is application/x-www-form-urlencoded .See my reply
– Nan Yu
Nov 23 at 2:25
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387908%2fcore-2-1-httprequest-body-is-empty-when-trying-to-read-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your type is "type is application/x-www-form-urlencoded" , so use [FromForm] attribute . Code below is for your reference :
.Net project :
HttpWebRequest request = null;
Uri uri = new Uri("https://localhost:44365/Home/Post");
UTF8Encoding encoding = new UTF8Encoding();
var postData = "thing1=hello";
postData += "&thing2=world";
byte bytes = encoding.GetBytes(postData);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.UseDefaultCredentials = true;
using (Stream writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
//return true;
}
else
{
// return false;
}
}
catch(Exception e)
{
}
On your .net Core Project :
[HttpPost]
public string Post([FromForm]AcceptValue acceptValue)
{
//do other stuff
return "";
}
public class AcceptValue {
public string thing1 { get; set; }
public string thing2 { get; set; }
}
Actually we are trying to hack our code. The problem is that the data cannot be sent in this way. var postData = "thing1=hello"; postData += "&thing2=world"; .Net code is the old one and we are in situation not to change that. Data that is sent can be any format and we need that in .net core solution. Any changes should be done in .net core solution side. We are in tricky position. :(
– Amrutha
Nov 23 at 6:39
add a comment |
Your type is "type is application/x-www-form-urlencoded" , so use [FromForm] attribute . Code below is for your reference :
.Net project :
HttpWebRequest request = null;
Uri uri = new Uri("https://localhost:44365/Home/Post");
UTF8Encoding encoding = new UTF8Encoding();
var postData = "thing1=hello";
postData += "&thing2=world";
byte bytes = encoding.GetBytes(postData);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.UseDefaultCredentials = true;
using (Stream writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
//return true;
}
else
{
// return false;
}
}
catch(Exception e)
{
}
On your .net Core Project :
[HttpPost]
public string Post([FromForm]AcceptValue acceptValue)
{
//do other stuff
return "";
}
public class AcceptValue {
public string thing1 { get; set; }
public string thing2 { get; set; }
}
Actually we are trying to hack our code. The problem is that the data cannot be sent in this way. var postData = "thing1=hello"; postData += "&thing2=world"; .Net code is the old one and we are in situation not to change that. Data that is sent can be any format and we need that in .net core solution. Any changes should be done in .net core solution side. We are in tricky position. :(
– Amrutha
Nov 23 at 6:39
add a comment |
Your type is "type is application/x-www-form-urlencoded" , so use [FromForm] attribute . Code below is for your reference :
.Net project :
HttpWebRequest request = null;
Uri uri = new Uri("https://localhost:44365/Home/Post");
UTF8Encoding encoding = new UTF8Encoding();
var postData = "thing1=hello";
postData += "&thing2=world";
byte bytes = encoding.GetBytes(postData);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.UseDefaultCredentials = true;
using (Stream writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
//return true;
}
else
{
// return false;
}
}
catch(Exception e)
{
}
On your .net Core Project :
[HttpPost]
public string Post([FromForm]AcceptValue acceptValue)
{
//do other stuff
return "";
}
public class AcceptValue {
public string thing1 { get; set; }
public string thing2 { get; set; }
}
Your type is "type is application/x-www-form-urlencoded" , so use [FromForm] attribute . Code below is for your reference :
.Net project :
HttpWebRequest request = null;
Uri uri = new Uri("https://localhost:44365/Home/Post");
UTF8Encoding encoding = new UTF8Encoding();
var postData = "thing1=hello";
postData += "&thing2=world";
byte bytes = encoding.GetBytes(postData);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.UseDefaultCredentials = true;
using (Stream writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
//return true;
}
else
{
// return false;
}
}
catch(Exception e)
{
}
On your .net Core Project :
[HttpPost]
public string Post([FromForm]AcceptValue acceptValue)
{
//do other stuff
return "";
}
public class AcceptValue {
public string thing1 { get; set; }
public string thing2 { get; set; }
}
answered Nov 21 at 7:51
Nan Yu
6,0952651
6,0952651
Actually we are trying to hack our code. The problem is that the data cannot be sent in this way. var postData = "thing1=hello"; postData += "&thing2=world"; .Net code is the old one and we are in situation not to change that. Data that is sent can be any format and we need that in .net core solution. Any changes should be done in .net core solution side. We are in tricky position. :(
– Amrutha
Nov 23 at 6:39
add a comment |
Actually we are trying to hack our code. The problem is that the data cannot be sent in this way. var postData = "thing1=hello"; postData += "&thing2=world"; .Net code is the old one and we are in situation not to change that. Data that is sent can be any format and we need that in .net core solution. Any changes should be done in .net core solution side. We are in tricky position. :(
– Amrutha
Nov 23 at 6:39
Actually we are trying to hack our code. The problem is that the data cannot be sent in this way. var postData = "thing1=hello"; postData += "&thing2=world"; .Net code is the old one and we are in situation not to change that. Data that is sent can be any format and we need that in .net core solution. Any changes should be done in .net core solution side. We are in tricky position. :(
– Amrutha
Nov 23 at 6:39
Actually we are trying to hack our code. The problem is that the data cannot be sent in this way. var postData = "thing1=hello"; postData += "&thing2=world"; .Net code is the old one and we are in situation not to change that. Data that is sent can be any format and we need that in .net core solution. Any changes should be done in .net core solution side. We are in tricky position. :(
– Amrutha
Nov 23 at 6:39
add a comment |
You should use the [FromBody] attribute.
It will be this:
[HttpPost]
public string Post([FromBody] object body) {
// do stuff here
}
1
This doesnt help. [FromBody] for "application/x-www-form-urlencoded" request gives 415 unsupported media type error in .net core.
– Amrutha
Nov 21 at 16:47
@Amrutha , FromBody is used for json type , you should use FromForm if the type is application/x-www-form-urlencoded .See my reply
– Nan Yu
Nov 23 at 2:25
add a comment |
You should use the [FromBody] attribute.
It will be this:
[HttpPost]
public string Post([FromBody] object body) {
// do stuff here
}
1
This doesnt help. [FromBody] for "application/x-www-form-urlencoded" request gives 415 unsupported media type error in .net core.
– Amrutha
Nov 21 at 16:47
@Amrutha , FromBody is used for json type , you should use FromForm if the type is application/x-www-form-urlencoded .See my reply
– Nan Yu
Nov 23 at 2:25
add a comment |
You should use the [FromBody] attribute.
It will be this:
[HttpPost]
public string Post([FromBody] object body) {
// do stuff here
}
You should use the [FromBody] attribute.
It will be this:
[HttpPost]
public string Post([FromBody] object body) {
// do stuff here
}
answered Nov 20 at 15:15
kjwdamme
264
264
1
This doesnt help. [FromBody] for "application/x-www-form-urlencoded" request gives 415 unsupported media type error in .net core.
– Amrutha
Nov 21 at 16:47
@Amrutha , FromBody is used for json type , you should use FromForm if the type is application/x-www-form-urlencoded .See my reply
– Nan Yu
Nov 23 at 2:25
add a comment |
1
This doesnt help. [FromBody] for "application/x-www-form-urlencoded" request gives 415 unsupported media type error in .net core.
– Amrutha
Nov 21 at 16:47
@Amrutha , FromBody is used for json type , you should use FromForm if the type is application/x-www-form-urlencoded .See my reply
– Nan Yu
Nov 23 at 2:25
1
1
This doesnt help. [FromBody] for "application/x-www-form-urlencoded" request gives 415 unsupported media type error in .net core.
– Amrutha
Nov 21 at 16:47
This doesnt help. [FromBody] for "application/x-www-form-urlencoded" request gives 415 unsupported media type error in .net core.
– Amrutha
Nov 21 at 16:47
@Amrutha , FromBody is used for json type , you should use FromForm if the type is application/x-www-form-urlencoded .See my reply
– Nan Yu
Nov 23 at 2:25
@Amrutha , FromBody is used for json type , you should use FromForm if the type is application/x-www-form-urlencoded .See my reply
– Nan Yu
Nov 23 at 2:25
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387908%2fcore-2-1-httprequest-body-is-empty-when-trying-to-read-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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