如何通过 x-APi-key 使用 rest Sharp
How to pass x-APi-key using rest Sharp
我正在尝试发送 api 密钥作为 header 在 Rest sharp 中,这就是我正在尝试的,
我得到 {"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;
}
我在 Python 中得到了相同的请求,哪个工作正常我如何在 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)
如果数据是在查询参数中发送的,那么你这里有一个错误:
request.Resource = string.Format("/v3/members/email={0}&password={1}&terms_and_conditions=true", email, password);
应该是
request.Resource = string.Format("/v3/members?email={0}&password={1}&terms_and_conditions=true", email, password);
我正在阅读 python request.post 文档数据是正文而不是查询。所以你需要创建
request.AddBody(new { A = "foo", B = "bar" })
这个 psot 也很有用
看起来你的 C# 中有一个错误已经指向你,但它需要是 members?email
而不是 members/email
如果你正在处理 AWS Api 网关后者会注册为一个完全不同的端点,这可能会导致向您抛出各种奇怪的状态代码,403 在这种情况下实际上可能是可以接受的。
接下来,我将使用以下方法代替向 url 添加参数:
request.AddParameter(string name, string value);
你上面写的一个例子是
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;
}
我正在尝试发送 api 密钥作为 header 在 Rest sharp 中,这就是我正在尝试的,
我得到 {"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;
}
我在 Python 中得到了相同的请求,哪个工作正常我如何在 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)
如果数据是在查询参数中发送的,那么你这里有一个错误:
request.Resource = string.Format("/v3/members/email={0}&password={1}&terms_and_conditions=true", email, password);
应该是
request.Resource = string.Format("/v3/members?email={0}&password={1}&terms_and_conditions=true", email, password);
我正在阅读 python request.post 文档数据是正文而不是查询。所以你需要创建
request.AddBody(new { A = "foo", B = "bar" })
这个 psot 也很有用
看起来你的 C# 中有一个错误已经指向你,但它需要是 members?email
而不是 members/email
如果你正在处理 AWS Api 网关后者会注册为一个完全不同的端点,这可能会导致向您抛出各种奇怪的状态代码,403 在这种情况下实际上可能是可以接受的。
接下来,我将使用以下方法代替向 url 添加参数:
request.AddParameter(string name, string value);
你上面写的一个例子是
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;
}