Body 使用不记名身份验证时未发送
Body not sent when using Bearer Authentication
我有一个 winform 客户端,它使用 Zalando 的 Connexion 和 OpenAPI 从 Python Flask API 中获取数据 3。
客户端使用 Net Framework 4.8。当我发送带有授权 header 的 POST 请求时,body 没有被发送,所以我从服务器收到错误 400。我已经检查了 API 端收到的数据,我还使用 Flask 创建了一个空白项目,仅输出它作为请求接收的内容,而 body 不存在。检查 Visual Studio 上的内容显示 body,但它从未到达 API 服务器。
如果我不输入授权 header,它就可以正常工作。它也适用于 GET,WITH header。
这就是我在客户端设置令牌的方式:
public void SetToken(string token) {
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
这是我的客户端默认构造函数:
public class RestClient{
private readonly HttpClient Client;
public RestClient {
Client = new HttpClient();
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
}
我已经搜索了很多,然后在这里询问有同样问题的人,但找不到 post。
我还看到几乎所有示例都将 form/urlencoded
用于 POST 而不是 application/json
但我想这是格式的简单选择,在使用身份验证时似乎不是必需的。
我正在使用:
- Visual Studio 2019
- Net Framework 4.8(也尝试使用 4.7)
- Python 3.7.2
- 烧瓶 1.1.1
- 连接 2.3.0
还尝试了 API 测试套件,使用请求库在 Python 上创建了 Bearer Authorization,它从那里工作正常...
编辑:
根据要求添加我的 post 代码:
public HttpResponseMessage Post(string path, HttpContent content, int maxRetries = 0)
{
if (maxRetries < 0)
{
throw new ArgumentException("maxRetries cannot be less than 0");
}
int attemptsMade = 0;
int maxAttempts = maxRetries + 1;
bool workCompletedSuccessfully = false;
bool attemptsRemain = true;
HttpResponseMessage response = null;
while (!workCompletedSuccessfully && attemptsRemain)
{
attemptsMade++;
attemptsRemain = attemptsMade < maxAttempts;
try
{
response = Client.PostAsync(path, content).GetAwaiter().GetResult();
if (response.IsSuccessStatusCode)
{
workCompletedSuccessfully = true;
}
}
catch (Exception e)
{
if (!attemptsRemain)
{
throw e;
}
}
}
return response;
}
这就是我从服务中调用它的方式:
private const string PATH = "person";
public PersonService(RestClient rest)
{
_rest = rest;
}
public HttpResponseMessage AddNew(Person person)
{
var personJson = JsonConvert.SerializeObject(person);
using (var content = new StringContent(personJson, Encoding.UTF8, "application/json"))
{
var result = _rest.Post($"api/{PATH}", content);
return result;
}
}
您的不记名令牌(您传递给 SetToken 方法的字符串)是否包含换行符?这可能会导致该问题。
我有一个 winform 客户端,它使用 Zalando 的 Connexion 和 OpenAPI 从 Python Flask API 中获取数据 3。 客户端使用 Net Framework 4.8。当我发送带有授权 header 的 POST 请求时,body 没有被发送,所以我从服务器收到错误 400。我已经检查了 API 端收到的数据,我还使用 Flask 创建了一个空白项目,仅输出它作为请求接收的内容,而 body 不存在。检查 Visual Studio 上的内容显示 body,但它从未到达 API 服务器。 如果我不输入授权 header,它就可以正常工作。它也适用于 GET,WITH header。 这就是我在客户端设置令牌的方式:
public void SetToken(string token) {
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
这是我的客户端默认构造函数:
public class RestClient{
private readonly HttpClient Client;
public RestClient {
Client = new HttpClient();
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
}
我已经搜索了很多,然后在这里询问有同样问题的人,但找不到 post。
我还看到几乎所有示例都将 form/urlencoded
用于 POST 而不是 application/json
但我想这是格式的简单选择,在使用身份验证时似乎不是必需的。
我正在使用:
- Visual Studio 2019
- Net Framework 4.8(也尝试使用 4.7)
- Python 3.7.2
- 烧瓶 1.1.1
- 连接 2.3.0
还尝试了 API 测试套件,使用请求库在 Python 上创建了 Bearer Authorization,它从那里工作正常...
编辑: 根据要求添加我的 post 代码:
public HttpResponseMessage Post(string path, HttpContent content, int maxRetries = 0)
{
if (maxRetries < 0)
{
throw new ArgumentException("maxRetries cannot be less than 0");
}
int attemptsMade = 0;
int maxAttempts = maxRetries + 1;
bool workCompletedSuccessfully = false;
bool attemptsRemain = true;
HttpResponseMessage response = null;
while (!workCompletedSuccessfully && attemptsRemain)
{
attemptsMade++;
attemptsRemain = attemptsMade < maxAttempts;
try
{
response = Client.PostAsync(path, content).GetAwaiter().GetResult();
if (response.IsSuccessStatusCode)
{
workCompletedSuccessfully = true;
}
}
catch (Exception e)
{
if (!attemptsRemain)
{
throw e;
}
}
}
return response;
}
这就是我从服务中调用它的方式:
private const string PATH = "person";
public PersonService(RestClient rest)
{
_rest = rest;
}
public HttpResponseMessage AddNew(Person person)
{
var personJson = JsonConvert.SerializeObject(person);
using (var content = new StringContent(personJson, Encoding.UTF8, "application/json"))
{
var result = _rest.Post($"api/{PATH}", content);
return result;
}
}
您的不记名令牌(您传递给 SetToken 方法的字符串)是否包含换行符?这可能会导致该问题。