1

I'm posting this object with json

[DataContract(Namespace = "http://localhost:30366/avtorji")]
public class Avtor
{
    [DataMember]
    public int idAvtor { get; set; }
    [DataMember]
    public string ime { get; set; }
    [DataMember]
    public string priimek { get; set; }
    [DataMember]
    public DateTime datum_rojstva { get; set; }

    public Avtor() { }
}

This is my request:

{
  "idAvtor": 4,
  "ime": "M Vaqqas",
  "priimek": "Noob",
  "datum_rojstva": "2014-01-03 23:28:56"
}

My function is

public void DodajAvtorja(Avtor avtor)
{
    string connString = null;

    try
    {
        connString = "server=localhost;userid=;password;database=";
        MySqlConnection conn = new MySqlConnection(connString);
        MySqlCommand comm = conn.CreateCommand();
        //avtor.datum_rojstva.ToString("yyyy-MM-dd HH:mm:ss")
        comm.CommandText = "INSERT INTO avtor (idAvtor, ime, priimek, datum_rojstva)"
            +" VALUES (@idAvtor, @ime, @priimek, @datum_rojstva)";

        comm.Parameters.AddWithValue("@idAvtor", avtor.idAvtor);
        comm.Parameters.AddWithValue("@ime", avtor.ime);
        comm.Parameters.AddWithValue("@priimek", avtor.priimek);
        comm.Parameters.AddWithValue("@datum_rojstva", avtor.datum_rojstva);

        conn.Open();

        int dodanih = comm.ExecuteNonQuery();
        if (dodanih > 0)
        {
            UriTemplateMatch match = WebOperationContext.Current.IncomingRequest.UriTemplateMatch;

            UriTemplate template = new UriTemplate("/avtor/{idAvtor}");
            Uri novAvtorUri = template.BindByPosition(match.BaseUri, avtor.idAvtor.ToString());
            WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(novAvtorUri);
        }
        conn.Close();
    }
    catch (Exception)
    {

        throw;
    }
}

It gives me an error "400 Bad Request" before it goes to function. I'm assuming this is because of DateTime property. Can you even send datetime that way? And what would be the solution?

Thank you for help!

dbc
  • 104,963
  • 20
  • 228
  • 340
klemsi123
  • 103
  • 1
  • 11
  • 1
    I think I need more information here. Bad Request could mean a lot of things. I can create a web service right now to return Bad Request if you forget to send a specific header I don't like. Is that the only message in the error? Do you own the web service you're posting to? – KSib Apr 26 '17 at 15:56
  • Yes I own the service and I edited the question – klemsi123 Apr 26 '17 at 16:05
  • If you go to the web service you're posting to and debug, it doesn't narrow down which line the error is happening on? I can't imagine the client will get all of the information (at least it probably shouldn't?) – KSib Apr 26 '17 at 16:07
  • What exact line is throwing the error? – KSib Apr 26 '17 at 16:13
  • Does your service have any data annotation? Are you using web api, mvc or wcf? – Jordi Ruiz Apr 26 '17 at 16:17
  • There is no line because it doestn't go into the function DodajAvtorja which it should. I'm using wcf – klemsi123 Apr 26 '17 at 16:21
  • It has problem with datetime because other stuff works – klemsi123 Apr 26 '17 at 16:22
  • What do you mean, there's no line? The app runs, I assume? How far can you go on the client until the error is thrown? – KSib Apr 26 '17 at 16:27
  • 1
    WCF uses `DataContractJsonSerializer` for JSON serialization. According to the [docs](https://msdn.microsoft.com/en-us/library/bb412170(v=vs.110).aspx#Anchor_3) this serializer uses the following format for `DateTime`: *DateTime values appear as JSON strings in the form of "/Date(700000+0500)/", where the first number (700000 in the example provided) is the number of milliseconds in the GMT time zone, regular (non-daylight savings) time since midnight, January 1, 1970.* Try formatting your `"datum_rojstva"` in this manner. – dbc Apr 26 '17 at 16:37
  • 1
    Thank you @dbc that is nice. Now it works! – klemsi123 Apr 26 '17 at 16:47
  • It's that strange format – klemsi123 Apr 26 '17 at 16:47
  • Another option is to define datum_rostjva as String, then use DateTime.Parse to convert it to a DateTime object. – Mike Taverne Apr 27 '17 at 00:35

1 Answers1

1

WCF uses DataContractJsonSerializer for JSON serialization. According to the docs this serializer uses the following format for DateTime:

DateTime values appear as JSON strings in the form of "/Date(700000+0500)/", where the first number (700000 in the example provided) is the number of milliseconds in the GMT time zone, regular (non-daylight savings) time since midnight, January 1, 1970. The number may be negative to represent earlier times. The part that consists of "+0500" in the example is optional and indicates that the time is of the Local kind - that is, should be converted to the local time zone on deserialization. If it is absent, the time is deserialized as Utc. The actual number ("0500" in this example) and its sign (+ or -) are ignored.

Thus you need to format your "datum_rojstva" in this manner:

{
  "datum_rojstva": "/Date(1388791736000)/",
  "idAvtor": 4,
  "ime": "M Vaqqas",
  "priimek": "Noob"
}

For help formatting such a date see for instance How do I format a Microsoft JSON date?.

For some history of why Microsoft chose this format, see An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET: ASP.NET AJAX: Inside JSON date and time string.

dbc
  • 104,963
  • 20
  • 228
  • 340