admin 管理员组文章数量: 1086019
I have an API that receives a string token. This API has a Post
method, and is expecting the string in the body:
[HttpPost("refreshToken")]
[Produces(MediaTypeNames.Application.Json)]
public async Task<ActionResult<JwtTokenDTO>> RefreshToken([FromBody] string token)
{
// ...
}
But I cannot figure out how to send this string to the API.
I have tried using
AddStringBody
AddParameter
AddJsonBody
My code:
private async Task<(bool Success, JwtTokenDTO? Token)> RefreshToken(string? refreshToken)
{
RestRequest request = new RestRequest("api/refreshToken");
//request.AddStringBody(refreshToken, DataFormat.Json);
//request.AddParameter("token", refreshToken, ParameterType.RequestBody);
//request.AddParameter("token", refreshToken, ParameterType.GetOrPost);
//request.AddJsonBody(new { Token = refreshToken }); // This works with complex object in the Api request.
request.AddQueryParameter("token", refreshToken, true); // This works removing the [FromBody] in the Api.
var response = await _client.ExecutePostAsync<ApiResponse<JwtTokenDTO>>(request);
// ...
}
The only thing it seems to work is
- Removing
[FromBody]
from the API method and send the value in the query string (AddQueryParameter
) - Or using a complex object in the API and send the value with
AddJsonBody
In Postman, I can easily send the string as shown here:
How can I send the string using in API and a [FromBody]
string token?
Thanks
I have an API that receives a string token. This API has a Post
method, and is expecting the string in the body:
[HttpPost("refreshToken")]
[Produces(MediaTypeNames.Application.Json)]
public async Task<ActionResult<JwtTokenDTO>> RefreshToken([FromBody] string token)
{
// ...
}
But I cannot figure out how to send this string to the API.
I have tried using
AddStringBody
AddParameter
AddJsonBody
My code:
private async Task<(bool Success, JwtTokenDTO? Token)> RefreshToken(string? refreshToken)
{
RestRequest request = new RestRequest("api/refreshToken");
//request.AddStringBody(refreshToken, DataFormat.Json);
//request.AddParameter("token", refreshToken, ParameterType.RequestBody);
//request.AddParameter("token", refreshToken, ParameterType.GetOrPost);
//request.AddJsonBody(new { Token = refreshToken }); // This works with complex object in the Api request.
request.AddQueryParameter("token", refreshToken, true); // This works removing the [FromBody] in the Api.
var response = await _client.ExecutePostAsync<ApiResponse<JwtTokenDTO>>(request);
// ...
}
The only thing it seems to work is
- Removing
[FromBody]
from the API method and send the value in the query string (AddQueryParameter
) - Or using a complex object in the API and send the value with
AddJsonBody
In Postman, I can easily send the string as shown here:
How can I send the string using in API and a [FromBody]
string token?
Thanks
Share Improve this question edited Mar 30 at 12:46 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 30 at 12:30 Marlon FezMarlon Fez 514 bronze badges2 Answers
Reset to default 0According to the comments #1270802845 and #1271225067 on RestRequest post fails when using AddBody with string. This worked fine in 107.3.0. #1886,
it should be worked to send the string as request body with:
request.AddJsonBody(refreshToken);
or
request.AddParameter(new JsonParameter("", refreshToken));
When you want to send a string in the request body, such as an token, you can use the AddBody
or AddStringBody
method and set the content type to text/plain
. The following alternatives are possible:
request.AddBody("Example token", ContentType.Plain);
or
request.AddHeader("Content-Type", "text/plain");
request.AddBody("Example token");
or
request.AddStringBody("Example token", ContentType.Plain);
The AddStringBody
method also has an overload with a DataFormat
enum as parameter type (as you tried), which supports Xml
, Json
and Binary
format, but does not have an explicit value for plain text, it works when set to None
however.
request.AddStringBody("Example token", DataFormat.None);
When the content type has not been set correctly it will result in a response returning status code 415, stating the request failed because of an unsupported media type.
本文标签: cHow to send a simple string in a body of a Post request in RestSharpStack Overflow
版权声明:本文标题:c# - How to send a simple string in a body of a Post request in RestSharp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1743989178a2514403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论