Search code examples
c#pythonrestrestsharp

How to pass x-APi-key using rest Sharp


I am Trying to send api key as header in Rest sharp, This is what I am trying,

I am getting {"status":403,"message":"Forbidden"}

public static IRestResponse CreateNewUser(string enviroment, string email, string password)
        {
            NameValueCollection Env = ValidateEnv(enviroment);
            string baseurlenv = Env["baseApiURL"];
            var enviroments = new EndPointProviders();
            var Client = new RestClient();
            Client.BaseUrl = new Uri(baseurlenv);
            var request = new RestRequest(Method.POST);
            request.Resource = string.Format("/v3/members/email={0}&password={1}&terms_and_conditions=true", email, password);
            request.AddHeader("x-api-key", "yxyxyxyx");
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            IRestResponse response = Client.Execute(request);
            Console.WriteLine(request);
            if (!IsReturnedStatusCodeOK(response))
            {
                throw new HttpRequestException("Request issue -> HTTP code:" + response.StatusCode);
            }
            return response;

        }

I managed to get the same request in Python Which works fine how can I do the same thing in Rest sharp?

import requests

# Create User/

headers = {
    'Host': 'host..',
    'x-api-key': 'yxyxyxyx',
    'content-type': 'application/x-www-form-urlencoded',
}

data = 'email=example18@gmail.com&password=example1&terms_and_conditions=true'

response = requests.post('https://sampeurl/v3/members', headers=headers, data=data)


print(response.content)

Solution

  • it looks like you have an error in your C# that was already pointed to you but it needs to be members?email not members/email If you are dealing with AWS Api Gateways the latter would register as a totally different endpoint and that can cause all sorts of weird status codes to get thrown at you, 403 being one that might actually be acceptable in that case.

    Next instead of adding parameters to the url as you are, I would use the following method:

    request.AddParameter(string name, string value);
    

    an example from what you wrote above would be

    public static IRestResponse CreateNewUser(string enviroment, string email, string password)
            {
                NameValueCollection Env = ValidateEnv(enviroment);
                string baseurlenv = Env["baseApiURL"];
                var enviroments = new EndPointProviders();
                var Client = new RestClient();
                Client.BaseUrl = new Uri(baseurlenv);
                var request = new RestRequest(Method.POST);
    
                // Resource should just be the path
                request.Resource = string.Format("/v3/members);
    
                // This is how to add parameters
                request.AddParameter("email", email);
                request.AddParameter("password", password);
                request.AddParameter("terms-and-conditions", "true");
    
                // This looks correct assuming you are putting your actual x-api-key here
                request.AddHeader("x-api-key", "yxyxyxyx");
    
                // There is a chance you need to configure the aws api gateway to accept this content type header on that resource. Depends on how locked down you have it
                request.AddHeader("content-type", "application/x-www-form-urlencoded");
                IRestResponse response = Client.Execute(request);
                Console.WriteLine(request);
                if (!IsReturnedStatusCodeOK(response))
                {
                    throw new HttpRequestException("Request issue -> HTTP code:" + response.StatusCode);
                }
                return response;
    
            }