go-dropbox
go-dropbox copied to clipboard
Cannot upload file without ClientModified
Problem
Cannot upload file without ClientModified. Dropbox API return "500 Internal server error"
Reason 1. If type is time.Time, omitempty doesn't working
http://stackoverflow.com/questions/32643815/golang-json-omitempty-with-time-time-field
http://ideone.com/Uapadk
package main
import (
"encoding/json"
"fmt"
"time"
)
type StringStruct struct {
Val string `json:"val,omitempty"`
}
type IntStruct struct {
Val int `json:"val,omitempty"`
}
type TimeStruct struct {
Val time.Time `json:"val,omitempty"`
}
func main() {
{
a := &StringStruct{}
b, _ := json.Marshal(a)
fmt.Println(string(b))
}
{
a := &IntStruct{}
b, _ := json.Marshal(a)
fmt.Println(string(b))
}
{
a := &TimeStruct{}
b, _ := json.Marshal(a)
fmt.Println(string(b))
}
}
output
{}
{}
{"val":"0001-01-01T00:00:00Z"}
Reason 2. "0001-01-01T00:00:00Z" is invalid date
client_modified : "0001-01-01T00:00:00Z"
$ curl -i -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer <my-token>" \
--header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Matrices.txt\",\"mode\": \"add\",\"mute\": false, \"client_modified\":\"0001-01-01T00:00:00Z\"}" \
--header "Content-Type: application/octet-stream" \
--data-binary @local_file.txt
HTTP/1.1 500 Internal Server Error
Server: nginx
Date: Fri, 13 Jan 2017 16:20:38 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Dropbox-Request-Id: 0b2875fd8a0069a1791d5517d5c3f44f
X-Robots-Tag: noindex, nofollow, noimageindex
client_modified : "0001-01-01T00:00:00Z"
$ curl -i -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer <my-token>" \
--header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Matrices.txt\",\"mode\": \"add\",\"mute\": false, \"client_modified\":\"2001-01-01T00:00:00Z\"}" \
--header "Content-Type: application/octet-stream" \
--data-binary @local_file.txt
HTTP/1.1 409 path/conflict/file/
Server: nginx
Date: Fri, 13 Jan 2017 16:21:14 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-Dropbox-Request-Id: fa466b7446442dfef16521036c59e089
X-Robots-Tag: noindex, nofollow, noimageindex
{"error_summary": "path/conflict/file/", "error": {".tag": "path", "reason": {".tag": "conflict", "conflict": {".tag": "file"}}, "upload_session_id": "AAAAAAAADm1RHB9OZkctbw"}}
Before 2017 Jan 04, if client_modified is "0001-01-01T00:00:00Z", it works. After 2017 Jan 04, if client_modified is "0001-01-01T00:00:00Z", it return 500 internal server error. Maybe dropbox modify their API.
Solution
A. use *time.Time
- pros : simple
- cons : interface will change. there are many
time.Time + omitemptyin library.
B. write custom marshal function
- pros : trustable, can keep interface
- cons : annoying task. there are many
time.Time + omitemptyin library.
C. Remove key and value if JSON value is "2001-01-01T00:00:00Z"
example
- Input string :
{"path": "/Homework/math/Matrices.txt","mode": "add","mute": false, "client_modified":"2001-01-01T00:00:00Z"} - If value is
2001-01-01T00:00:00Z, it is default value of time.Time - Erase key and value from JSON
- Output string :
{"path": "/Homework/math/Matrices.txt","mode": "add","mute": false}
- pros : can keep interface
- cons : some kinds of hack
Summary
I don't know which solution do you like. I can't make pull request. So I make issue.