Home > Software engineering >  Why is the response returning 204
Why is the response returning 204

Time:11-25

When I add items to my cart, I send back the cart as reponse.No Problem.These Codes work in locale(my computer) return response 200 . But When I send the project to hosting. When I add items to my cart response 204(Null) . I didn't solve this problem.

                BACKEND(.Net Core 3.1 ASPNET Web Api)

enter image description here

               FrontEnd 
import { message } from "antd";

export type ApiSuccessResponse<T> = {
    ok: true;
    data: T;
};

export type ApiErrorResponse = {
    ok: false;
    status: number;
    message: string;
};

export type ApiResponse<T> = ApiSuccessResponse<T> | ApiErrorResponse;

export type RequestMethod = "DELETE" | "GET" | "PATCH" | "POST" | "PUT";

export async function request<T>(
    uri: string,
    method?: RequestMethod,
    body?: any
): Promise<ApiResponse<T>> {
    let token = localStorage.getItem("token");
     
    const response = await fetch(`/api/${uri}`, {
        method: method || "GET",
        headers: {
            "Content-Type": "application/json; charset=utf-8",
            Authorization: "Bearer "   (token || ""),
        },
        body: body ? JSON.stringify(body) : undefined,
    });
    if (!response.ok) {
        if (response.status === 401) {
            localStorage.clear();
            window.location.reload();
        } else if (response.status === 500) {
            const text = await response.text();
            if (text && text.length) {
                message.error(JSON.parse(text).message);
            }
        }
        return {
            ok: false,
            status: response.status,
            message: response.statusText,
        };
    }

    const text = await response.text();

    if (text && text.length > 0) {
        const data = JSON.parse(text);
        return { ok: true, data: data as T };
    }

    return { ok: true, data: null! };
}
```

CodePudding user response:

204 means that the endpoint succeeded but without content. As far as I know, you are returning null. Please deconstruct your last line and await for the result, like this:

var authServiceUnit = await CreateAuthServiceUnitAsync(_masterServiceUnit);
var cartDto = await authServiceUnit.CarService.GetCartAsync(CarId,KullaniciId, UserType);
return cartDto; // set here a breakpoint

cartDto should be null.

  • Related