dpik71879 2015-05-11 05:58
浏览 13

使用转到盒子帐户上传文件和文件夹

For the program below I get the below error. It will be helpful if anyone helps me to fix my errors. Thanks in advance.

func upload() {
    fmt.Println("dfxfgcghvjbjhiiiiiiiiiiiiiiiiiii")
    apiUrl := "https://upload.box.com/"
    resource := "api/2.0/files/content"
    data := url.Values{}
    data.Add("access_token", accessobj.Access_token)
    authbear := "Bearer "
    authbear += accessobj.Access_token

    u, _ := url.ParseRequestURI(apiUrl)
    u.Path = resource
    urlStr := fmt.Sprintf("%v", u)
    client := &http.Client{}
    fmt.Println(urlStr)

    f, err := ioutil.ReadFile("C:\\Users\\vembu\\Desktop\\hi.txt")
    ioutil.WriteFile("hi.txt", f, 0x777)
    r, _ := http.NewRequest("POST", urlStr, bytes.NewBuffer(f))
    r.Header.Add("Authorization", "Bearer "+accessobj.Access_token)
    r.Header.Add("attributes",
        "{\"name\":\"hi.txt\",\"parent\":{\"id\":\"3098791209\"}}")
    r.Header.Add("file", "hi.txt")
    fmt.Println(r)
    if err != nil {
        fmt.Println("error......:", err)
    }

    resp, err1 := client.Do(r)
    if err1 != nil {
        fmt.Println("error:", err1)
    }
    fmt.Println("uploading")
    fmt.Println(resp)

    re, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("errorrrrr:", err)
    }
    fmt.Println(string(re))
}

I get the following error:

I face &{405 Method Not Allowed 405 HTTP/1.1 1 1
map[Allow:[GET, OPTIONS, HEAD] Content-Type:[text/html;charset=UTF-8]
Content-Length:[0] Date:[Thu, 12 Mar 2015 13:07:32 GMT] Age:[0]
Connection:[keep-alive]
Server:[ATS]] 0xc08200b8c0 0 [] false map[] 0xc08201f2b0 0xc082060980}
  • 写回答

1条回答 默认 最新

  • duancai7002 2015-05-11 06:07
    关注

    The problem is that you try to do an HTTP POST request:

    r, _ := http.NewRequest("POST", urlStr, bytes.NewBuffer(f))
    

    But the server does not allow/support this as stated in the response error (only GET, OPTIONS and HEAD methods are allowed):

    Method Not Allowed 405 HTTP/1.1 1 1 map[Allow:[GET, OPTIONS, HEAD]

    According to the box.com API to upload a file using POST you need to use a multi-part form upload request.

    You can use the multipart package to create a multipart request with a file.

    Here is an example how to do it (incomplete/untested code):

    buf := &bytes.Buffer{}
    mw := multipart.NewWriter(buf)
    defer mw.Close()
    f, err := os.Open("C:\\Users\\vembu\\Desktop\\hi.txt")
    if err != nil {
        // Handle error
    }
    defer f.Close()
    ff, err := mw.CreateFormFile("name", "hi.txt")
    if err != nil {
        // Handle error
    }
    if _, err = io.Copy(ff, f); err != nil {
        // Handle error
    }
    
    // TODO: INCLUDE OTHER FIELDS/PARAMS IN URL
    r, err := http.NewRequest("POST", "https://upload.box.com/api/2.0/files/content", buf)
    if err != nil {
        // Handle error
    }
    r.Header.Set("Content-Type", mw.FormDataContentType())
    // TODO: ADD YOUR OTHER HEADER FIELDS
    
    // Do the call: upload file
    client := &http.Client{}
    resp, err := client.Do(r)
    if err != nil {
        // Handle error
    }
    
    if resp.StatusCode != http.StatusOK {
        fmt.Printf("Error: %v", resp.Status)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错