Main Site Documentation

Trouble POST ing to MVC WebAPI


#1

I am posting data to my asp.net webapi server.

I can post data via Fiddler (see image below) and the json payload comes across intact, but when I post data via netmf the body value is null.

Here is my posting code.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“http://localhost:36142/api/values”);
request.Method = “Post”;
request.UserAgent = “netmf”;
request.ContentType = “application/json; charset=utf-8”;
string body = “{“Email”:“tps@ tps.com”,“PhoneNumber”:“512-1234”,“RowKey”:“20141210:221577”,“PartitionKey”:“spencer”}”;
byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
request.ContentLength = bodyBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(bodyBytes, 0, bodyBytes.Length);
WebResponse response = request.GetResponse();
HttpWebResponse hResponse = (HttpWebResponse)response;
stream.Close();
Debug.Print("---------------------------------------");
Debug.Print(hResponse.StatusCode.ToString());
Debug.Print(hResponse.StatusDescription);
Debug.Print("---------------------------------------");
response.Close();

Thank you for your help on this.
Terrence


#2

Hi Terrence,

the best thing is to check out my Azure Mobile Services library which is literally a Web Api Call: https://github.com/mobernberger/netmf-azure-mobile-services

Here is a excerpt of the call adepted to your Api Call:


using (var request = (HttpWebRequest)WebRequest.Create(_finalUri))
            {
                request.Method = "POST";
                request.Accept = "application/json";

                string body = "{\"Email\":\"tps@ tps.com\",\"PhoneNumber\":\"512-1234\",\"RowKey\":\"20141210:221577\",\"PartitionKey\":\"spencer\"}";

				//prepare request
				byte[] byteData = Encoding.UTF8.GetBytes(body);
				request.ContentLength = byteData.Length;
				request.ContentType = "application/json";
				request.UserAgent = "Micro Framework";

				using (Stream postStream = request.GetRequestStream())
				{
					postStream.Write(byteData, 0, byteData.Length);
				}
					
                //wait for the response
                using (var response = (HttpWebResponse)request.GetResponse())
                using (var stream = response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    Debug.Print(reader.ReadToEnd());
                }
            }

Michael


#3

Michael, thanks for your reply.

I tried your great code and I am getting the same results.

The value in the webapi is still null.

Any other ideas?

Terrence


#4

Hi Terrence,

could you give me the source of your Web Api to check it on my own or give me short directions how you created your Web Api site to test it.

Thanks,
Michael


#5

Michael, it is a very simple webapi.
Create a webapi project and go to the values controller and look at the Post method.
Thank you for your help.

Here is the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;

namespace WebApplication1.Controllers
{
public class ValuesController : ApiController
{
public void Post([FromBody]string value)
{
// value is always null, except when using fiddler.
}
}
}


#6

Michael, any luck on creating the webapi and posting a json value?

Stuck and can’t wait to move forward.

Currently I am trying to figure out how to get the post run through fiddler so I can inspect it, but no luck so far. Have you been able to do that?

Thanks for your help.

Terrence


#7

Hi Terrence,

I have tried the Api with Fiddler and I also getting Null on the value when I make the request via Fiddler.


#8

I know using statement should take care of that, but can you try explicitly calling postStream.Close()


#9

Arch, note in my code from the first post, I do a stream.close();

Are you able to post data to a webservice via netmf?

Stream stream = request.GetRequestStream();
stream.Write(bodyBytes, 0, bodyBytes.Length);
WebResponse response = request.GetResponse();
HttpWebResponse hResponse = (HttpWebResponse)response;
stream.Close();

Thanks for your thoughts.
Terrence


#10

Michael, you need an opening { on that body, and you also need to single quote the start and end.

‘{…}’

Try that.

Are you posing data to the cloud in some other way?

Thanks for your help.
Terrence


#11

@ terrence - Oh, I see I was looking at Michael’s snippet.

Can you move stream.Close up, right after you write to the stream.


#12

I will give that a try. I will have to do that tonight as I am at my day job now.

Thanks for your input.

Terrence


#13

Hi Terrence,

after some reading and testing I have found the solution. You have to change the content type and add a “=” before the actual JSON data.
Check out the forum post where I found it here: http://forums.asp.net/t/1883467.aspx?The+received+value+is+null+when+I+try+to+Post+to+my+Web+Api

And here is the working code:

using (var request = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost:8705/api/values")))
            {
                request.Method = "POST";
                request.Accept = "application/x-www-form-urlencoded";

                string body = "={\"Email\":\"tps@ tps.com\",\"PhoneNumber\":\"512-1234\",\"RowKey\":\"20141210:221577\",\"PartitionKey\":\"spencer\"}";

                byte[] byteData = Encoding.UTF8.GetBytes(body);
                request.ContentLength = byteData.Length;
                request.ContentType = "application/x-www-form-urlencoded";
                request.UserAgent = "Micro Framework";

                using (Stream postStream = request.GetRequestStream())
                {
                    postStream.Write(byteData, 0, byteData.Length);
                }

                using (var response = (HttpWebResponse)request.GetResponse())
                using (var stream = response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    Debug.Print(reader.ReadToEnd());
                }
            }

Michael


#14

Michael, it seems that that post was trying to figure out how to post with fiddler.

I am totally able to post with fiddler, I just can’t post with netmf.

I will try the suggestion after work today and let you know how it went.

Thanks again for your help.

Terrence


#15

Hi Terrence,

I know but this also works for NETMF. I have already tried it with a Console Application from where the Code sample above is.

Michael


#16

Good deal. I will get back with you in 4 hours.

Thanks.
Terrence


#17

Michael, YOU ARE THE MAN !!!

It worked. I can’t thank you enough for solving this problem for me.

Thank you, Thank you, Thank you.

Terrence


#18

@ mobernberger - How do I mark your reply as “the answer”?


#19

I am looking to use something similar in my project.

I would like to know if this would work with a Cerbuino Bee using Wi-Fi (Wifly XN-RV) ?

Thank you. Microu.


#20

@ microu -
I am sorry I do not have any experience with a Cebruino Bee.