First a bit more info which i should have included in the first place. First note that this example is just a simplified version of what i'm actually trying to do.
- So first, I'm posting the form data from a codebehind file using c# (not from an html page or via json).
- My uri is /api/newUser
- My route for the action looks pretty much the same: "api/newUser"
- My controller action looks like this: publicHttpResponseMessage<Contact>PostUser()
So in regards to digitalpacman's response of just adding a param to the action, that is fine, but in order to do that, won't that force me to change my uri and route? (I'm trying to keep the uri as /api/newUser). So instead of posting to a uri of /api/newUser,
wouldn't this force my uri to be "api/newUser/firstName".
Also, i tried to post via the codebehind using the application/x-www-form-urlencoded type, but without success. So i'm doing something like:
-----
HttpWebRequest req = null;
HttpWebResponse res = null;
const string url = "http://localhost:8090/api/newUser";
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.Headers.Add("MyRequest", url);
req.ContentLength = 900;
var sw = new StreamWriter(req.GetRequestStream());
sw.Write("testing");
sw.Close();
res = (HttpWebResponse)req.GetResponse();
Stream responseStream = res.GetResponseStream();
var streamReader = new StreamReader(responseStream);
//Read the response into an xml document
var soapResonseXmlDocument = new XmlDocument();
soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());
apiResponse.Text = soapResonseXmlDocument.InnerXml;
jjonesca
Member
2 Points
7 Posts
Re: How to obtain POST Request values in the API
Apr 24, 2012 08:03 PM|LINK
First a bit more info which i should have included in the first place. First note that this example is just a simplified version of what i'm actually trying to do.
- So first, I'm posting the form data from a codebehind file using c# (not from an html page or via json).
- My uri is /api/newUser
- My route for the action looks pretty much the same: "api/newUser"
- My controller action looks like this: public HttpResponseMessage<Contact> PostUser()
So in regards to digitalpacman's response of just adding a param to the action, that is fine, but in order to do that, won't that force me to change my uri and route? (I'm trying to keep the uri as /api/newUser). So instead of posting to a uri of /api/newUser, wouldn't this force my uri to be "api/newUser/firstName".
Also, i tried to post via the codebehind using the application/x-www-form-urlencoded type, but without success. So i'm doing something like:
-----
HttpWebRequest req = null; HttpWebResponse res = null; const string url = "http://localhost:8090/api/newUser"; req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.Headers.Add("MyRequest", url); req.ContentLength = 900; var sw = new StreamWriter(req.GetRequestStream()); sw.Write("testing"); sw.Close(); res = (HttpWebResponse)req.GetResponse(); Stream responseStream = res.GetResponseStream(); var streamReader = new StreamReader(responseStream); //Read the response into an xml document var soapResonseXmlDocument = new XmlDocument(); soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd()); apiResponse.Text = soapResonseXmlDocument.InnerXml;-----
Any clues as to what i'm doing wrong?