Trouble POST ing to MVC WebAPI

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

Hi Terrence,

the best thing is to check out my Azure Mobile Services library which is literally a Web Api Call: GitHub - mobernberger/netmf-azure-mobile-services: A class library for communicating from .NET Micro Framework with 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

1 Like

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

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

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.
}
}
}

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

Hi Terrence,

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

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

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

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

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

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

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

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

3 Likes

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

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

1 Like

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

Thanks.
Terrence

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

1 Like

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

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.

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