How to send String Array ( string [] )in Request Params for REST API in C#?

rich brown
rich brown used Ask the Experts™
How to send String Array ( string [] )in Request Params for REST API in C#?


This does not work.

public string GetLocation(string [] myItems)
{

           //var item = myItems;
            var item = "[\"Alabama\", \"Texas\"]";   //Test    
            var regionCode = "South";


            var url = $"https://MyLocation.com/api/GetLocation?MySearch={ item }&region={ regionCode }";

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";

            var strResult = string.Empty;

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                strResult = result;
            }

            string myMsgResult = strResult;


}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
How to send String Array ( string [] )in Request Params for REST API in C#?


This does not work. I need to pass string array parameter in body of the Rest API.  How do I pass data in a Rest API "POST"  from the  Body of the HttpRequest?

public string GetLocation(string [] myItems)
{

           //var item = myItems;
            var item = "[\"Alabama\", \"Texas\"]";   //Test    
            var regionCode = "South";


            var url = $"https://MyLocation.com/api/GetLocation?MySearch={ item }&region={ regionCode }";

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";

            var strResult = string.Empty;

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                strResult = result;
            }

            string myMsgResult = strResult;


}
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018
Hi Rich,

Have you considered sending JSON? Is it possible for you to convert data and send it over as a JSON object?

Regards,
Chinmay.
Ryan ChongSoftware Team Lead
Do you have the documentation of end point : https://MyLocation.com/api/GetLocation ?

you need to know what exactly need to be posted, so try read it if it's available.

for a quick debugging, you may consider to use Postman to send out your tests.

https://www.getpostman.com/
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

I'm able to get my C# code to work with just the Rest "POST" of the Query parameters but  how do I "POST" the  REST API with Query parameters and a Body parameters?
     
My example:

endpoint with query parameter

api/GetLocation?MySearch=City

and

Body parameter
["North", ''South"]
Software Team Lead
you could try something like this:

private string GetLocation(string regionCode, List<string> myItems)
        {
            //Do necessary try.. catch

            var postData = JsonConvert.SerializeObject(myItems);

            var url = $"https://MyLocation.com/api/GetLocation?MySearch=" + regionCode;

            var data = Encoding.ASCII.GetBytes(postData);

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/json"; // "application /x-www-form-urlencoded";
            httpWebRequest.ContentLength = data.Length;

            using (var stream = httpWebRequest.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var strResult = string.Empty;

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                strResult = result;
            }
            string myMsgResult = strResult;
            return myMsgResult;
        }

Open in new window


to implement:

List<string> itm = new List<string>();
itm.Add("North");
itm.Add("South");
string x = GetLocation("City", itm);

Open in new window

Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial