I'm building a web Api to catalog my firms bug report onto a server, and I'm having trouble with the post request that I'm doing.
As I've coded the clients they should be either sending bug reports that are formatted like this
public partial class JitCollect {
public DateTime? Timestamp { get; set; }
public string Jit { get; set; }
public int? ProjectId{ get; set; }
}
or they could be sending strings with status reports like "OK" or whatever. My main problem is that if I send the bug reports as "application/x-www-form-urlencoded"
and prepending a '=' to my body like I saw online, I lose the DateTime format that NewtonSoft is expecting on the other side:
before "2018-08-14T08:50:17.5608444+02:00"
after "2018-08-14T08:50:17.5608444 02:00"
I could hardcode something to put the '+' back but that's beside the point, I'm interested in how to properly accomplish what I'm trying to do.
if I instead try to send the data as "application/json", I always get empty data on the other side even specifying the from body attribute and the object type (which is not ideal for me because I want to be able to send plain strings as well)
[HttpPost]
public string Post([FromBody] List < JitCollect > jsonPost)
any idea what is the best way to do this? here's one of the many post functions I tried to use
public static void postRequest(string data, string address) {
using (var client = new HttpClient()) {
client.BaseAddress = new Uri(address);
data = $"={data}";
//client.DefaultRequestHeaders.Add("token", token);
var buffer = System.Text.Encoding.UTF8.GetBytes(data);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
//byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = client.PostAsync("", byteContent).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
}
}