C
C#2y ago
Playboi17

❔ 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();
}
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();
}
4 Replies
Playboi17
Playboi172y ago
Essentially exactly the same
mtreit
mtreit2y ago
"createmany" is not a full URI
new Uri(_httpClient.BaseAddress.AbsoluteUri + "/createmany")
new Uri(_httpClient.BaseAddress.AbsoluteUri + "/createmany")
that produces a full URI, your second example doesn't do that at all. So I would not call that essentially exactly the same
Anton
Anton2y ago
also make the options static
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.