tmenier / Flurl

Fluent URL builder and testable HTTP client for .NET
https://flurl.dev
MIT License
4.23k stars 387 forks source link

Send post json will add \r\n #757

Closed hyfree closed 1 year ago

hyfree commented 1 year ago

Please provide a complete description of the bug and a code sample (or better yet, a unit test) that reproduces it. example code

 string json = $$"""
            {
                "version": "2.0",
                "name": "aaaa",
            }
            """;
var rec = await url.AllowAnyHttpStatus.PostJsonAsync(json).ReceiveString();

wireshark http cap

POST /api HTTP/1.1
Host: test.mytest.com:80
Content-Length: 144
Accept-Encoding: gzip, deflate
Connection: close
Content-Type: application/json; charset=UTF-8

"{\r\n    \"version\": \"2.0\",\r\n    \"name\": \"aaaaa\"\r\n }"

How to send this message using flurl

POST /api HTTP/1.1

Content-Type: application/json
User-Agent: PostmanRuntime/7.32.3
Accept: */*
Postman-Token: c7bf162b-d02f-4fbf-a6d2-1e28800ad64b
Host: api.test.cn:80
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 116

{
    "version": "2.0",
    "name": "aaaaa"
}

Here's one of the solutions I found:

https://stackoverflow.com/questions/56315725/httpclient-post-request-with-headers-and-body

thank !!

tmenier commented 1 year ago

I assume you know what \r\n\ means? I've never used Wireshark but I imagine it just displays linebreaks escaped like this so they always fit on one line. If I'm wrong about that then I guess I don't know the answer, but it's not Flurl bug and perhaps a better question for a Wireshark forum or Stack Overflow.

hyfree commented 1 year ago

Thanks, I understand \r\n

If there is a text

"""hello
word"""

then its Ascii code is 68 65 6c 6c 6f 0d 0a 77 6f 72 64 Its length is 11 The flurl send is 68 65 6c 6c 6f 5c 72 5c 6e 77 6f 72 64 The two-character "0d 0a" becomes the four-byte "5c 72 5c 6e."

Guess: flurl does a urlencoud on the string。

Can I control this behavior?

Thank you very much for your answer.

tmenier commented 1 year ago

I don't know the exact answer, but I will say that Flurl defers the heavy lifting to HttpClient, so low-level behavior like this tends to reflect HttpClient's behavior. If you can repro with HttpClient, post a question to Stack Overflow. You'll reach a much bigger audience than framing it as a Flurl question.