0

I got one more question related to api I'm working on :p

My problem is that the application i made to read the data works perfectly when the url is all passed, but if i want to pass the general url + parameters it doesn't...

Code with Full URL private void btn_Api_Click(object sender, EventArgs e) {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://MyIP/api/Employee/?FirstName=Jorge");
        request.Method = "POST";
        string postData = "";
        byte[] data = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        request.Accept = "application/json";
        request.ContentLength = data.Length;


        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(data, 0, data.Length);
        }

        try
        {
            using (WebResponse response = request.GetResponse())
            {
                var responseValue = string.Empty;


                // grab the response  
                using (var responseStream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(responseStream))
                    {
                        responseValue = reader.ReadToEnd();
                    }
                }
                if (responseValue != "")
                {
                    string _txtFileNew = @"C:\Users\Jorge.Leite\Documents\teste\" + txt_First.Text + ".txt"; //+txt_Last.Text + ".txt";
                    StreamWriter _srEannew = new StreamWriter(_txtFileNew);
                    _srEannew.WriteLine(responseValue);
                    _srEannew.Close();

                }
            }
        }
        catch (WebException ex)
        {
            // Handle error
        }

Code with Url + parameters later PS: postData is passing the correct string ("?FirstName=Jorge") Jorge is the input Name

private void btn_Api_Click(object sender, EventArgs e) {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("MyIP/api/Employee/");
        request.Method = "POST";
        string postData = string.Format("?FirstName=" + txt_First.Text);
        byte[] data = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        request.Accept = "application/json";
        request.ContentLength = data.Length;

When using this code it has an exception server not found 404

I really don't know what going on with this :\

Thank you in advance

Jorge
  • 71
  • 1
  • 13

1 Answers1

0

Web API with POST methods do not work as smooth as with GET methods.

Check this other link to know a bit more Reading FromUri and FromBody at the same time

Community
  • 1
  • 1
DanielCuadra
  • 970
  • 9
  • 17
  • I'm on fire all questions I made were all explained with simple things. By some reason I don't remember now, i took out the [FromBody] from the post methods, i updated them and published and now all works fine. God I feel so stupid for being all afternoon on this Thank you a lot. – Jorge Apr 01 '14 at 16:32