0
votes

I have this code:

var advertApiModel = _mapper.Map<AdvertModel>(model);

var jsonModel = JsonConvert.SerializeObject(advertApiModel);
var response = await _client.PostAsync(new Uri(
                   $"{_baseAddress}/create"),
                   new StringContent(jsonModel, Encoding.UTF8, 
                   "application/json"
               )).ConfigureAwait(false);
var createAdvertResponse = await response.Content.ReadAsAsync<CreateAdvertResponse>().ConfigureAwait(false);
var advertResponse = _mapper.Map<AdvertResponse>(createAdvertResponse);

When I run this and trace it, it keeps getting the error Invalid expression term 'string' at var response and the terminal displays:

info: System.Net.Http.HttpClient.IAdvertApiClient.ClientHandler[101] Received HTTP response after 246.85940000000002ms - InternalServerError System.Net.Http.HttpClient.IAdvertApiClient.ClientHandler: Information: Received HTTP response after 246.85940000000002ms - InternalServerError

1
Apparently the request you're sending leads to an error on the server. Without knowing the API and what it expects (and what exactly you are sending), I doubt there's anything to help you with. - germi
"Invalid expression term 'string'" - where does this show up? - CodeCaster
@CodeCaster It shows when i hover over encoding. when it tries to create StringContent, it reads jsonModel but when i hover over "application/json" is gives that error and then everything breaks - Cosmin Mihalache
Remove new StringContent and use jsonModel variable instead. - LinkedListT

1 Answers

0
votes

Welcome to StackOverflow Cosmin Mihalache.

I think using your variable jsonModel instead of the new StringContent may help you:

var response = await _client.PostAsync(new Uri(
                   $"{_baseAddress}/create"),
                   jsonModel
               )).ConfigureAwait(false);