[BUG] POST Http request has extra data (hex numbers) in body
Describe the bug
Http POST request with serialized body content contains unexpected hexadecimal numbers. Numbers wrap the whole regular content. See bellow to captured request:
POST https://<someservice.local>/api/license/630D6EC0-364C-4062-8F8A-6DAD47D32440 HTTP/1.1
Host: <someservice.local>
Authorization: Bearer <jwt token>
Accept: application/json
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
3D
{"licItemId":"licenseId1","activateAfter":"licenseId2"}
0
Steps To Reproduce
- Run some proxy to capture request generated by .NET app
- Run following code with .NET 5.0, Refit 6.0.94 and Windows
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Refit;
namespace RefitPostIssue
{
public interface IClient
{
[Post("/api/license/{tenantId}")]
Task Activate(string tenantId, [Body] Request request);
}
public class Request
{
[JsonProperty("licItemId")]
public string LicenseId { get; set; }
[JsonProperty("activateAfter")]
public string ActivateAfter { get; set; }
}
class Program
{
static async Task Main(string[] args)
{
var httpClient = RestService.CreateHttpClient("https://<someservice.local>", new RefitSettings(new NewtonsoftJsonContentSerializer()));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "<jwt token>");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var client = RestService.For<IClient>(httpClient, new RefitSettings(new NewtonsoftJsonContentSerializer()));
await client.Activate("630D6EC0-364C-4062-8F8A-6DAD47D32440", new Request()
{
LicenseId = "licenseId1",
ActivateAfter = "licenseId2"
});
}
}
}
- Body of captured request contains unexpected content
Expected behavior
Serialized body should contain only expected serialized content.
Screenshots
Environment
- OS: Windows 11
- Device: PC
- Version: Refit 6.0.38 - 6.0.94
- Working Version: Refit 6.0.24
Additional context
@vlaskal I have found the root cause for the issue. The body attribute has a property Buffered that is set to false by default, in which case the HttpRequestMessage Content is initialized from the PushStreamContent which causes the additional body encoding with hex characters like you described.
Fortunately, you can opt out from that behaviour by setting the property to true, in which case the content will be set as it is.
Please change the IClient in your sample to
public interface IClient
{
[Post("/api/license/{tenantId}")]
Task Activate(string tenantId, [Body(buffered: true)] Request request);
}
@artyomabrahamyan thank you for hint. It will help for sure.
Is this behaviour exected as default? I think that my case is not special and I think something should change to be more straight forward.
@vlaskal
This is the default behaviour actually, since by default refit is designed in the way that there is no need to load everything into the memory and the request body can be formed for example from the file. You may see more information here: Body Content. However, the stream has its start and end markers, 0 for example means the end(another hex like 3D or 3F is usually put into the begining).
If you find yourself uncomfortable with this default behaviour you may configure your desired behaviour from the config also
https://github.com/reactiveui/refit/blob/246ee8d9989c29092fdaba6821d5b3098a5ccf9e/Refit/RefitSettings.cs#L90 by setting Buffered to true.
Hope that helps.
Hi Thanks for help. I am closing it.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.