Click here to Skip to main content
15,881,709 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
27 Feb 2014CPOL 44.2K   7   3
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:

CLIENT Code

C#
private void button1_Click(object sender, EventArgs e)
{
    ProcessRESTPostXMLFileAsStr
    ("http://Platypus:28642/api/Platypi/PostArgsAndXMLFileAsStr?serialNum=192837465&siteNum=42");
}

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)
            {
                sb.AppendLine(line);
            }
        }
        // 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

C#
[Route("api/Platypi/PostArgsAndXMLFileAsStr")]
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.

License

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


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

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, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
GeneralMy vote of 5 Pin
John B Oliver19-Mar-14 11:19
John B Oliver19-Mar-14 11:19 
QuestionHow about using HttpClient.. Pin
seee sharp27-Feb-14 19:50
seee sharp27-Feb-14 19:50 
AnswerRe: How about using HttpClient.. Pin
B. Clay Shannon28-Feb-14 3:35
professionalB. Clay Shannon28-Feb-14 3:35 

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

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