Bhavin Varsur
Bhavin Varsur

Reputation: 327

.NetCore list of string in HttpWebRequest GET with Query

I have a GET API in which expecting fields from query parameters. which is look like this

public List<Sms> SendSms([FromQuery] string apiKey, [FromQuery] string accountKey, [FromQuery] string appId,[FromQuery] string userId, [FromQuery] string message, [FromQuery] bool isHighSpeed = false,[FromQuery] List<string> mobile)
{
     // do something    
}

In this API I am expecting mobile in the list of strings. when i am calling this api by web request in my other project. i am adding mobile number but it takes nothing and taking a System.Collections.Generic.List'1[System.String] something like that. I don't know how to give list of string in query parameter in the httpweb request. here is the webrequest :

public virtual bool SendSms(SmsResponse sms)
{
    try
    {
        var message  = sms.message;
        var mobile = sms.mobile;
        var apiKey = Config.GetSection("Sms:apiKey").Value;
        var userId = Config.GetSection("Sms:userId").Value;
        var accountKey = Config.GetSection("Sms:accountKey").Value;
        var appId = Config.GetSection("fusionAuth:Client_Id").Value;
        var query = $"apiKey={apiKey}&accountKey={accountKey}&userId={userId}&appId={appId}&message={message}&mobile={mobile}&isHighSpeed={false}";

        string createreq = string.Format($"{Config.GetSection("Sms:Url").Get<string>()}SMS/SendSms?{query}");
        HttpWebRequest request = WebRequest.Create(createreq) as HttpWebRequest;
        request.Method = "GET"; 
        request.ContentType = "application/json";
        request.Accept = "application/json; charset=utf-8"; 

        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception(String.Format("Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription));
            }
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            List<SmsResponse> Data = JsonConvert.DeserializeObject<List<SmsResponse>>(responseFromServer);
            if (string.IsNullOrEmpty(Data[0].extMessageId))
            { 
                return false;
            }
        }
        return true;
    }
    catch (WebException ex)
    {
        var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
        return false;
    }
}

Upvotes: 0

Views: 1369

Answers (1)

Jason Pan
Jason Pan

Reputation: 22029

Please check SmsResponse class. Which type of mobile ?

enter image description here

The way you get mobile params is correct.

And you also need to make sure the format of request url.

 var query = $"apiKey={apiKey}&accountKey={accountKey}&userId={userId}&appId={appId}&message={message}&mobile={mobile}&isHighSpeed={false}";

1. If the type of mobile is string, you code should like below:

enter image description here

2. If the type of mobile is List<string>, you code should like below:

enter image description here

Upvotes: 1

Related Questions