reactiveui / refit

The automatic type-safe REST library for .NET Core, Xamarin and .NET. Heavily inspired by Square's Retrofit library, Refit turns your REST API into a live interface.

Home Page:https://reactiveui.github.io/refit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Sent empty request body

soarwing52 opened this issue ยท comments

Describe the bug ๐Ÿž

using refit through http post request with body

this happened when I use Task.waitAll() with multiple api requests at once

while using the api with internal sevice in our private network, I got 400 Response status code does not indicate success: 400 (Bad Request).

while trying to fix it, I found that when I use a handler, if I await the content to be read, it can sent the body properly
await request.Content.ReadAsByteArrayAsync();

[Post("/api/staff/GetStaffBasic")] Task<ResponseGetStaffBasic> GetStaffBasic([Body] RequestGetStaffBasic req);

Model:

`
public class RequestGetStaffBasic
{
public RequestGetStaffBasic()
{
Token = string.Empty;
Detail = new RequestDetailGetStaffBasic();
}
public string Token { get; set; }
public DateTime Timestamp { get; set; }
public RequestDetailGetStaffBasic Detail { get; set; }
}

public class RequestDetailGetStaffBasic
{
    public List<RequestGetStaffBasicItem> Content { get; set; }
}

public class RequestGetStaffBasicItem
{
    public string UserName { get; set; }
    public int StaffSn { get; set; }
}

`

Response:

` public class ResponseGetStaffBasic
{
public ResponseGetStaffBasic()
{
Message = string.Empty;
Detail = new ResponseDetailGetStaffBasic();
}

    public DateTime Flag { get; set; }
    public bool IsSuccess { get; set; }
    public string Message { get; set; }
    public ResponseDetailGetStaffBasic Detail { get; set; }
}

public class ResponseDetailGetStaffBasic
{
    public ResponseDetailGetStaffBasic()
    {
        Content = new List<ResponseGetStaffBasicItem>();
    }
    public List<ResponseGetStaffBasicItem> Content { get; set; }
}

public class ResponseGetStaffBasicItem
{
    public int StaffSn { get; set; }
    public string UserName { get; set; }
    public int Department { get; set; }
    public string DepartmentName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ChineseName { get; set; }
    public string Email { get; set; }
}

`

Step to reproduce

  1. send a post request with refit
  2. found that it respond 400 error
  3. add LoggingHandler with await the content to be read and then send request
  4. found the body is properly sent

Reproduction repository

https://github.com/reactiveui/refit

Expected behavior

It should send the request body properly

Screenshots ๐Ÿ–ผ๏ธ

Snipaste_2023-08-17_10-24-55

IDE

No response

Operating system

Windows

Version

10

Device

pc

Refit Version

7.0.0

Additional information โ„น๏ธ

No response

the API I request to should be another dotnet backend, and I added
[Headers("Accept: /")]
to my request and it worked

I tried to turn off some of the default headers in postman and found the behavior

An update on the situation:
on some of the api calls, without

await request.Content.ReadAsByteArrayAsync()

some api calls will get empty body, but it has nothing to do with the header.

and in other siutation, the header
[Headers("Accept: */*")]
will fix

@soarwing52 I was facing similar issue, what worked for me was setting buffered:true in body attribute
[Body(true)] myobject