-2

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;
   }
}
fatcoq
  • 17
  • 3
  • Possible duplicate of [How to POST using HTTPclient content type = application/x-www-form-urlencoded](https://stackoverflow.com/questions/43158250/how-to-post-using-httpclient-content-type-application-x-www-form-urlencoded) – mjwills Aug 14 '18 at 10:26
  • I like to convert dates to an integer of some kind, preferably epoch. Human readable date formats shouldn't be a computer's burden :) – Davesoft Aug 14 '18 at 11:26
  • What is the value of `data`? – mjwills Aug 15 '18 at 02:08
  • What do you mean by **I lose the DateTime format that NewtonSoft is expecting on the other side**? Share us related code or steps. Try use postman to send request, will this issue happen? – Edward Aug 15 '18 at 09:08

1 Answers1

0

I made a test with asp.net core web api, it passes the datetime correctly.

  1. Controller

        [HttpPost]
    public object Post([FromBody]List<JitCollect> jsonPost)
    {
        var resul = jsonPost.FirstOrDefault().Timestamp.Value.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK");
        return new List<JitCollect>() {
            new JitCollect{ ProjectId = 1, Jit = "J1", Timestamp = DateTime.Now }
        };
    }
    
  2. Request from postman
    enter image description here

  3. Result From Controller

enter image description here

Edward
  • 28,296
  • 11
  • 76
  • 121