Click here to Skip to main content
14,979,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have my sendRequest method where I am passing my contentType, token and query params.
I have used a Dictionary<string, string> for my query params but I am getting an error. It shows AddQueryParameter takes string name, string value so I am confused as to why it can not accept a Dictionary. The error I get it
"No overload for method AddQueryParameter"


Also my request.AddParameter getting an error here of "Use of un assigned local variable". Should I assign my IRestRequest request variable to = null. Wont that cause nullpointerexception?


Here is what I have
private static IRestRequest sendRequest(string contentType, string token, Dictionary<string, string> paramsMap)
        {
            IRestRequest request;

           //if token is null no need to add the token. Some request requires token, 
            Some does not

            if (token != null)
            {
                request.AddParameter("Authorization", "Bearer" + token, 
                      ParameterType.HttpHeader);

            }

            if (!(paramsMap == null))
            {   
                request.AddQueryParameter(paramsMap);
            }

           

           if (contentType != null) //if contentType is not null then add content type
            {
                if (contentType.Equals("JSON"))
                {
                    request.AddHeader("Accept", "application/json");
                }
                else if (contentType.Equals("XML"))
                {
                    request.AddHeader("Accept", "application/xml");
                }
                else if (contentType.Equals("TEXT"))
                {
                    request.AddHeader("Accept", "text/plain");
                }
            }

            return request;
        }


What I have tried:

private static IRestRequest sendRequest(string contentType, string token, Dictionary<string, string> paramsMap)
        {
         IRestRequest request = null; //this may solve my local request variable problem

           //if token is null no need to add the token. Some request requires token, 
            Some does not

            if (token != null)
            {
                request.AddParameter("Authorization", "Bearer" + token, 
                      ParameterType.HttpHeader);

            }

            if (!(paramsMap == null))
            {   
                request.AddQueryParameter(paramsMap);
            }

           

           if (contentType != null) //if contentType is not null then add content type
            {
                if (contentType.Equals("JSON"))
                {
                    request.AddHeader("Accept", "application/json");
                }
                else if (contentType.Equals("XML"))
                {
                    request.AddHeader("Accept", "application/xml");
                }
                else if (contentType.Equals("TEXT"))
                {
                    request.AddHeader("Accept", "text/plain");
                }
            }

            return request;
        }
Posted
Updated 13-May-20 6:01am
v3

1 solution

You're trying to call a method which accepts two strings, but you're trying to pass in a Dictionary<string, string>.

Computers don't just "guess" what you meant to do; they do exactly what you tell them to do. If you tell it to call a method called AddQueryParameter passing in a Dictionary<string, string>, the computer will - correctly - tell you that there is no such method.

You need to iterate over the values in the dictionary and add them separately:
C#
if (paramsMap != null)
{
    foreach (KeyValuePair<string, string> pair in paramsMap)
    {
        request.AddQueryParameter(pair.Key, pair.Value);
    }
}


You're getting an "unassigned local variable" error because you haven't assigned a value to the request parameter. And no, you can't set it to null, because then you'd get a NullReferenceException at run-time. You need to create a new request instance and store it in that variable before you try to do anything with the variable.
   

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900