C#C
C#3y ago
Connor

❔ Why does one request work but the other doesn't when they're exactly the same.

private readonly JsonSerializerOptions JsonOptions = new () { ReferenceHandler = ReferenceHandler.Preserve};

// This works
public async Task CreateMany1(IEnumerable<TEntity> entities)
    {
        var request = new HttpRequestMessage()
        {
            Method = HttpMethod.Post,
            Content = JsonContent.Create(entities, new MediaTypeHeaderValue("application/json"), JsonOptions),
            RequestUri = new Uri(_httpClient.BaseAddress.AbsoluteUri + "/createmany"),
        };
        var response = await _httpClient.SendAsync(request);
        response.EnsureSuccessStatusCode();
    }

public async Task CreateMany2(IEnumerable<TEntity> entities)
    {
        var response = await _httpClient.PostAsJsonAsync("createmany", entities, JsonOptions);
        response.EnsureSuccessStatusCode();
    }
Was this page helpful?