Click here to Skip to main content
11,438,069 members (46,850 online)
Click here to Skip to main content

How to Pass a Large String to a Web API Method (Apart from the URI)

, 27 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Pass a long string to a Web API method (not as part of the URI query)

Passing S.O.U.S. "Under the Table" to a Web API Method

Once you know how, it's easy to pass a S.O.U.S. (String Of Unusual Size - see the movie "The Princess Bride" if you don't understand that) to a Web API method. Here's how:


private void button1_Click(object sender, EventArgs e)

private void ProcessRESTPostXMLFileAsStr(string uri)
    using (var client = new WebClient())
        client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        StringBuilder sb = new StringBuilder();
        using (StreamReader sr = new StreamReader(@"C:\Platypus\eXtraneousMothLepidoptera.XML"))
            String line;
            while ((line = sr.ReadLine()) != null)
        // I don't know why the prepended equals sign is necessary, but it is
        string allLines = "=" + sb.ToString();
        var result = client.UploadString(uri, "POST", allLines);

Obviously (hopefully), you need to replace the IP Address, Port, Controller name, and arg vals in the ProcessRESTPostXMLFileAsStr() arg, also the location of the file to read in that method.

Here, Now, is the SERVER (Web API App) Code

public void PostArgsAndXMLFileAsStr([FromBody] string value, string serialNum, string siteNum)
    string s = string.Format("{0}{1}{2}", value, serialNum, siteNum);
    // Parsing the contents of the stringified XML left as an exercise to the exorcist

You're Welcome

If you like this tip, respond with "as you wish" the next time your parent, child, spouse, or significant duckbilled platypus requests something of you. Again, see the aforelinked flick if your grokMeter is ululating.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

B. Clay Shannon
Founder "Across Time & Space"
United States United States
Ideaman and Coder at Across Time & Space, creator of "Mark Twain Central" at

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction:
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

GeneralMy vote of 5 Pin
John B Oliver19-Mar-14 12:19
memberJohn B Oliver19-Mar-14 12:19 
QuestionHow about using HttpClient.. Pin
Ashish Basran27-Feb-14 20:50
memberAshish Basran27-Feb-14 20:50 
AnswerRe: How about using HttpClient.. Pin
B. Clay Shannon28-Feb-14 4:35
professionalB. Clay Shannon28-Feb-14 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150506.1 | Last Updated 27 Feb 2014
Article Copyright 2014 by B. Clay Shannon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid