I have a requirement where I have been posting data to a web API as a json string in a POST request and the post method retrieves the data from the body. This works perfectly for most data, but it does not work when I include a long dash(—) as part of the data in any fields.
I have a Email class with some string fields and I am passing it to the API to save in the database. Here is how I am implementing the call:
public string PostNewEmailRecord(string APIEndpoint, CampaignWave Email) { string StrEmailId = string.Empty; _endpoint = APIEndpoint; try { string strData = JsonConvert.SerializeObject(Email); _client.Headers.Add(HttpRequestHeader.ContentType, "application/json"); _client.UploadString(APIEndpoint, _requestType, strData); } catch (Exception ex) { } return StrEmailId; } And here is the post method of Web API:
public void Post([FromBody]CampaignWave email) { try { using (var transaction = new TransactionScope()) { CampaignWaveRepository cr = new CampaignWaveRepository(); object objReturnValue = cr.Insert(email); transaction.Complete(); } } catch (Exception ex) { } finally { } } When I include a dash the API post method receives a null value as email.
Please help me how I can successfully pass the '—' without any issue. Thanks in advance.
client.Encoding = Encoding.UTF8;