[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
- send a post request with refit
- found that it respond 400 error
- add LoggingHandler with await the content to be read and then send request
- found the body is properly sent
Reproduction repository
https://github.com/reactiveui/refit
Expected behavior
It should send the request body properly
Screenshots ๐ผ๏ธ
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