0

I have an ApiController looking like this:

public class UploadController : ApiController
{
    public StatusModel PostXML(string languageCode, string username, string password, string xml)
    {
        ...
    }
}

And I'm trying to call this method from an external project like this:

public StatusModel UploadXML() {
    var client = new RestClient("");
    string url = "http://localhost:52335/api/upload/PostXML/de/" + HttpUtility.UrlEncode(TESTXML) + "/user/password";
    var request = new RestRequest(url, Method.POST);

    return client.Execute<StatusModel>(request).Data;
}

When the TESTXML variable is a simple text like "Test", the web api method gets called and the values transmitted, however as soon as I put any xml tag in it, even if it's only a single "<", it does not anymore despite my UrlEncoding. And not only is my web api function not called, the Ajax calling my UploadXML method jumps into the error function despite getting an http response 200.

After hours of trying to find a solution, I'm out of ideas. What am I doing wrong? How can I pass an XML-string as parameter in a URL?

Thanks

3
  • 1
    You have quite a few issues here, but generally you should be posting your data, not using a GET url, to create a post: domain.com/controller/action/some/data/here is a get request. domain.com/controller/action is your post URL, with the data in the post url form params. You would then bind your action to a model, and not to single params like you have done. Commented Nov 16, 2013 at 13:49
  • also, if you are using chrome, get the postman plugin, you should be testing your API and have the API ready before you even write a single line of code against it Commented Nov 16, 2013 at 13:52
  • I was geared to a different api that was set up like this, but they were obviously get requests. Good to know the difference in the URLs. I've rewritten the URL to http://localhost:52335/api/upload/PostXML?languageCode=de&username=user&password=password&xml=" + HttpUtility.UrlEncode(TESTXML); and now it works, thanks. If you care to have your answer marked, post it as answer Commented Nov 18, 2013 at 8:08

1 Answer 1

1

You have quite a few issues here, but generally you should be posting your data, not using a GET url, to create a post: domain.com/controller/action/some/data/here is a get request. domain.com/controller/action is your post URL, with the data in the post url form params. You would then bind your action to a model, and not to single params like you have done.

Here is some code I use in my baseclass to make all API calls, when calling the API from serverside code:

public string CreateApiRequest(string url, object model, bool isPost)
    {
        try
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "";
            if(model != null)
            {
                postData = Strings.SerializeToQueryString(model).TrimEnd(Convert.ToChar("&"));
            }

            byte[] data = encoding.GetBytes(postData);
            var serviceUrl = "http://" + HttpContext.Request.Url.Host + "/api/{0}";
            // create the post and get back our data stream
            var myRequest = (HttpWebRequest)WebRequest.Create(new Uri(string.Format(serviceUrl, url)));
            if (isPost)
            {
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.Accept = "application/json";

                myRequest.ContentLength = data.Length;
                Stream newStream = myRequest.GetRequestStream();
                // Send the data.
                newStream.Write(data, 0, data.Length);
                newStream.Close();
            }
            else
            {
                myRequest.Method = "GET";
                myRequest.Accept = "application/json";
            }


            // Get response  
            using (HttpWebResponse response = myRequest.GetResponse() as HttpWebResponse)
            {
                // Get the response stream  
                var reader = new StreamReader(response.GetResponseStream());

                // Read the whole contents and return as a string  
                var myString = reader.ReadToEnd();
                return myString;
            }
        }
        catch (Exception ex)
        {
            // handle error here, I have my own custom mailer
            throw;
        }
    }

and this is how I call it.

var model = JsonConvert.DeserializeObject<List<Address>>(CreateApiRequest(url: "Address", model: null, isPost: false));

hope this helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.