Issues sending datetime field with Retrofit to Django REST Framework endpoint

Using Retrofit I can send and receive data in order, including dates, but when I use @Multipart it stops working.

This is a log of my sent data:

Content-Disposition: form-data; name="post_date"
Content-Type: application/json; charset=UTF-8
Content-Length: 26
Content-Transfer-Encoding: binary
"2015-08-06T19:37:14.000Z"

      

Here is the error I am getting:

{"post_date": ["Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]"]}

      

If I omit the date_time field, all other fields are passed in correctly. I'm not entirely sure what I'm doing wrong here, the data I'm sending is in the date format that DRF expects, but that doesn't seem to be the case.

Edit: if I use Postman and post the values ​​that Retrofit claims to send it. I can copy and paste the date above (no quotes) and it only sends fine.

Edit 2: I figured out the problem, the quotes around the date value in my log not only in my log, they are added to the date the value was sent to the server. So ... how do I convince Retrofit that date values ​​don't need extra quotes around them?

Edit 3: It looks like the problem is that Gson is adding quotes to the date when it is formatted for JSON, and then Retrofit just sends the value that Gson provides. If you are sending a multi-page message, you don't need these quotes. Not sure how to do this by creating an issue on the Gitub Retrofit page. I know that Retrofit either fades quotes from strings before sending them as part of a multi-page message, or never adds them in the first place, it looks like the same strategy should be used for dates.

+3


source to share


1 answer


Extracting the answer from k4yaman's comment.

Got the same problem in my js file and I built my Json data over my own and let Django handle it. As shaping was the only reason in my case I am posting the format.



var today = new Date(); 
var dd = today.getDate(); 
var mm = today.getMonth()+1; //January is 0! 
var yyyy = today.getFullYear(); 
var hh = today.getHours(); 
var m = today.getMinutes(); 
var secs = today.getSeconds(); 
var now = yyyy+"-"+mm+"-"+dd+"T"+hh+":"+m+":"+secs

      

0


source







All Articles