refit
refit copied to clipboard
Refit.ApiException : Response status code does not indicate success: 500 (Internal Server Error)
Migrate to latest version 6.0.x
i always have an execption occured when using Put or Post verbs
I use console application in dotenet core 3.1 in orther to test Call Api usin Refit but without success.
the verbe GET works
here is my interface portion of code posing the probleme
[Put("/products/update/{id}")] Task update(int id,[Body] Produit produit);
and here the call of update methode
var nsAPI = RestService.For<IProductsApi>(someuri); // Update Product Produit prd = new Produit(); prd.Cb = "12345678900"; prd.Designation = "Customer Product"; await nsAPI.update(12, prd);
note that my API work fine with Postman and the Pakage RestSharp
I suspect this is probably down to the default json serialization being changed from Newtonsoft to System.Text.Json as the json produced by both is slightly different, and can require some adjustments. Alternatively, you can tell Refit to use Newtonsoft to serialize the json.
You could probably confirm if that's the case by simply switching it and trying again. Probably worth logging the request being made to check the actual JSON it is outputting as there's likely some things you can do to make System.Text.Json produce the output you need and have it work correctly.
See the docs here for more info: https://github.com/reactiveui/refit#breaking-changes-in-6x
GitHub
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. - reactiveui/refit
Like @james-s-tayler said, I update a project from 3.1 to 5.0 few days ago and i had to change some [JsonProperty("property")] from Newtonsoft to [JsonPropertyName("property")] from System.Text.Json.Serialization
that was on response object from GET call but maybe you have any explicit properties named on class Produit ?
I'm also having this issue after upgrading Refit to 6.0.x
The weird thing is that if you have a custom DelegatingHandler
and read the request there, it suddenly works.
public class SomeHandler : DelegatingHandler
{
public SomeHandler()
{
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// this resolves the issue!! WHY?
if (request.Content != null)
{
var requestString = await request.Content.ReadAsStringAsync();
}
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
}
Then you use that handler:
services.AddRefitClient<IMyApi>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://someuri.sample"))
.AddHttpMessageHandler<SomeHandler>();
It works mysteriously. Why?
I also already set my serializer to be Newtonsoft.Json. Still the same. But that magical await request.Content.ReadAsStringAsync();
in the DelegateHandler fixes the entire issue. What sorcery is this? :)
We also ran into this issue, wish I saw this sooner...
Btw, we were getting this in asp.net Core 3.1 app, and then I was able to reproduce it easily in a .netcore3.1 app test project.
One Post works ok, another Post method kept corrupting the payload, until added essentially empty delegating handler, that reads the request.Content, per @aiampogi .
@omon77 are you able to provide anything like stack trace or a minimal reproduction?
bump, still having this issue with a Xamarin Forms 5 app (Android), 500 error on POST, works fine with V5 of Refit.
@MuddyChrisA which lib are you using for body serialization ?
@Dashell Using Latest Refit.Newtonsoft.Json NuGet package with ContentSerializer set to NewtonsoftJsonContentSerializer.
@MuddyChrisA and have you tried with System.Text.Json which is the default serializer in refit V6 ?
when we upgrade to V6 qe got some errors with [JsonProperty("property")] from Newtonsoft because of that, but after changing for [JsonPropertyName("property")] from System.Text.Json.Serialization
that works fine
@Dashell I would like to try to migrate the serializer but it would take a lot of time, effort and testing for the project I'm working on right now, so not really an option at this time. Reverted to v5 and all works for the time being.
any news on this? why the reading of the request.Content
mysteriously fixes the issue? while it's a nice workaround, it can be annoying because it doesn't make sense.
I had something very similar - My Azure function was giving a 500 error and adding the request.Content.ReadAsStringAsync()
call in a Http handler fixed the issue. I did some before / after comparison and discovered that Refit 6 was sending the request with chunked encoding. Adding Buffered = true
to RefitSettings
fixed this for me based on the advice in this issue:
https://github.com/reactiveui/refit/issues/362
Tuve algo muy similar: mi función de Azure estaba dando un error 500 y al agregar la
request.Content.ReadAsStringAsync()
llamada en un controlador Http se solucionó el problema. Hice una comparación antes/después y descubrí que Refit 6 estaba enviando la solicitud con codificación fragmentada. AgregandoBuffered = true
paraRefitSettings
arreglar esto para mí basado en el consejo en este problema:#362
it works!! thanks