Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] POST Http request has extra data (hex numbers) in body #1242

Closed
vlaskal opened this issue Sep 20, 2021 · 5 comments
Closed

[BUG] POST Http request has extra data (hex numbers) in body #1242

vlaskal opened this issue Sep 20, 2021 · 5 comments
Labels

Comments

@vlaskal
Copy link

vlaskal commented Sep 20, 2021

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

  1. Run some proxy to capture request generated by .NET app
  2. 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"
            });
        }
    }
}
  1. 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 vlaskal added the bug label Sep 20, 2021
@artyomabrahamyan
Copy link

artyomabrahamyan commented Jun 30, 2022

@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);
    }

@vlaskal
Copy link
Author

vlaskal commented Jul 3, 2022

@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.

@artyomabrahamyan
Copy link

artyomabrahamyan commented Jul 3, 2022

@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

public bool Buffered { get; set; } = false;

by setting Buffered to true.

Hope that helps.

@vlaskal
Copy link
Author

vlaskal commented Oct 20, 2022

Hi Thanks for help. I am closing it.

@vlaskal vlaskal closed this as completed Oct 20, 2022
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants