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

Refit.ApiException : Response status code does not indicate success: 500 (Internal Server Error) #1122

Open
cmsdesigner opened this issue Mar 16, 2021 · 14 comments
Labels

Comments

@cmsdesigner
Copy link

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

@james-s-tayler
Copy link
Contributor

james-s-tayler commented Mar 19, 2021

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

@Dashell
Copy link
Contributor

Dashell commented Mar 19, 2021

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 ?

@aiampogi
Copy link

aiampogi commented Apr 6, 2021

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?

@aiampogi
Copy link

aiampogi commented Apr 7, 2021

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? :)

@omon77
Copy link

omon77 commented May 5, 2021

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 .

@james-s-tayler
Copy link
Contributor

@omon77 are you able to provide anything like stack trace or a minimal reproduction?

@MuddyChrisA
Copy link

MuddyChrisA commented Nov 15, 2021

bump, still having this issue with a Xamarin Forms 5 app (Android), 500 error on POST, works fine with V5 of Refit.

@Dashell
Copy link
Contributor

Dashell commented Nov 15, 2021

@MuddyChrisA which lib are you using for body serialization ?

@MuddyChrisA
Copy link

@Dashell Using Latest Refit.Newtonsoft.Json NuGet package with ContentSerializer set to NewtonsoftJsonContentSerializer.

@Dashell
Copy link
Contributor

Dashell commented Nov 15, 2021

@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

@MuddyChrisA
Copy link

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

@aiampogi
Copy link

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.

@JustinPealing
Copy link

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:

#362

@angelru
Copy link

angelru commented May 18, 2022

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. Agregando Buffered = truepara RefitSettings arreglar esto para mí basado en el consejo en este problema:

#362

it works!! thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants