Link to home
Start Free TrialLog in
Avatar of rich brown
rich brown

asked on

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

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;


}
Avatar of rich brown
rich brown

ASKER

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;


}
Avatar of Chinmay Patel
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.
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/
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"]
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.