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;


}
* arraysC#REST

Avatar of undefined
Last Comment
rich brown

8/22/2022 - Mon
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;


}
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.
Ryan Chong

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 started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
rich brown

ASKER
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
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rich brown

ASKER
Thanks.