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.