普通网友 2019-01-26 21:25
浏览 264
已采纳

Golang http.NewRequest总是返回404-邮递员还可以

I'm trying to write a post request function that sends a file to a client from a golang server. The code is cribbed a bit from here (golang POST data using the Content-Type multipart/form-data). For whatever reason I always get "404 page not found", even though the same request when done through postman is hitting the endpt and returning successfully. This is bizarre behaviour and I'm not quite certain how to debug. (I've hard coded the URL to hit a locally running server for test purposes.)

Here is my code:

func PostUpload(values map[string]io.Reader, URL string){
    fmt.Println("inside PostUpload in outbound")
    client := &http.Client{}
    var err error

    var b bytes.Buffer
    w := multipart.NewWriter(&b)
    for key, r := range values {
            var fw io.Writer
            if x, ok := r.(io.Closer); ok {
                    defer x.Close()
            }
            // Add an image file
            if x, ok := r.(*os.File); ok {
                    if fw, err = w.CreateFormFile(key, x.Name()); err != nil {
                            return
                    }
            } else {
                    // Add other fields
                    if fw, err = w.CreateFormField(key); err != nil {
                            return
                    }
            }
            if _, err = io.Copy(fw, r); err != nil {
                fmt.Println("there was an error")
                fmt.Println(err)
            }
        }
    w.Close()

    // Now that you have a form, you can submit it to your handler.
        // req, err := http.NewRequest("POST", URL, &b)
        req, err := http.NewRequest("POST", "http://127.0.0.1:5000/upload", &b)
    if err != nil {
                fmt.Println("there was an error on http.NewRequest in outbound post file upload")
                fmt.Println(err)
    }
    // Don't forget to set the content type, this will contain the boundary.
        req.Header.Set("Content-Type", w.FormDataContentType())


        requestDump, err := httputil.DumpRequest(req, true)
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(string(requestDump))


    // Submit the request
    resp, err := client.Do(req)
    if err != nil {
                fmt.Println("there was an error on client do in outbound post file upload")
                fmt.Println(err)
        }

        resp_body, _ := ioutil.ReadAll(resp.Body)
        var parsed map[string]interface{}
        err = json.Unmarshal(resp_body, &parsed)
        if err!=nil{
            if typeof(resp_body)=="[]uint8"{
                fmt.Println("received []uint8, converting and displaying")
                fmt.Print(string(resp_body))
            }else{
                fmt.Println("inside unmarshal error")
                fmt.Println(err)
            }
        }

    return
}

And here is the output in console:

api      | 21:16:09 app         | inside PostUpload in outbound
api      | 21:16:09 app         | POST /upload HTTP/1.1
api      | Host: 127.0.0.1:5000
api      | Content-Type: multipart/form-data; boundary=0ce8aee17e5d00524acd3875d8b4b41dba5f99d8a7796d56289e38f89c64
api      |
api      | --0ce8aee17e5d00524acd3875d8b4b41dba5f99d8a7796d56289e38f89c64
api      | Content-Disposition: form-data; name="file"; filename="/uploads/TIME^2019-01-26 21:16:09.413514444 +0000 UTCFILE^Any_Publishing.txt"
api      | Content-Type: application/octet-stream
api      |
api      | # © 2016 and later: Unicode, Inc. and others.
api      | # License & terms of use: http://www.unicode.org/copyright.html#License
api      | #
api      | # File: Any_Publishing.txt
api      | # Generated from CLDR
api      | #
api      |
api      | # Variables
api      | $single = \' ;
api      | $space = ' ' ;
api      | $double = \";
api      | $back = \` ;
api      |
api      | --0ce8aee17e5d00524acd3875d8b4b41dba5f99d8a7796d56289e38f89c64--
api      |
api      | 21:16:09 app         | received []uint8, converting and displaying
api      | 404 page not found

Does anyone see what is going wrong?

EDIT:

Someone suggested commenting out the dump on req as it may consume req leaving it empty for the request. I tried that with the same results as above.

Someone mentioned that they wanted to see the curl request of a successful upload so here it is:

patientplatypus:~/Downloads:15:48:28$curl -X POST -F "file=@./catpic.jpg" http://127.0.0.1:5000/upload
file uploaded successfully!

Full dump of curl with -v flag:

patientplatypus:~/Downloads:15:48:42$curl -v -X POST -F "file=@./catpic.jpg" http://127.0.0.1:5000/upload
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /upload HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 264915
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------48411aa948b523e2
>
< HTTP/1.1 100 Continue
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 27
< Server: Werkzeug/0.14.1 Python/3.6.5
< Date: Sat, 26 Jan 2019 22:12:19 GMT
<
* Closing connection 0
file uploaded successfully!

Someone mentioned that they wanted to see the server I was attempting to hit so here it is: https://github.com/patientplatypus/pythonServer

  • 写回答

2条回答 默认 最新

  • doubo3384 2019-01-26 23:40
    关注

    That 404 error string looks the same as the one generated by the Go standard library:

    Are you sure you're actually hitting the Flask application?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置