Golang http.NewRequest 总是 returns 404 - Postman ok
Golang http.NewRequest always returns 404 - Postman ok
我正在尝试编写一个 post 请求函数,将文件从 golang 服务器发送到客户端。代码从这里抄袭了一点 (golang POST data using the Content-Type multipart/form-data)。无论出于何种原因,我总是得到“404 页面未找到”, 即使 通过 postman 完成相同的请求时到达 endpt 并成功返回。这是奇怪的行为,我不太确定如何调试。 (出于测试目的,我已经将 URL 硬编码为命中本地 运行 服务器。)
这是我的代码:
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
}
这是控制台中的输出:
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
有人看到哪里出了问题吗?
编辑:
有人建议将 req 上的转储注释掉,因为它可能会消耗 req 而将请求留空。我试过了,结果和上面一样。
有人提到他们想查看上传成功的 curl 请求,所以这里是:
patientplatypus:~/Downloads:15:48:28$curl -X POST -F "file=@./catpic.jpg" http://127.0.0.1:5000/upload
file uploaded successfully!
带有 -v 标志的 curl 的完整转储:
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!
有人提到他们想看看我试图攻击的服务器,所以这里是:https://github.com/patientplatypus/pythonServer
从表面上看,您没有提出相同的请求。 Curl(感谢包括在内)设置一个表单值,而你的 go 设置 2。在你的 curl 中它是 'file' 但在 go 中,'filename'。我在输出的内容配置行中看到了这一点。使用 -v 卷曲还可能包含有关它发送 f 的内容的更多信息。
该 404 错误字符串看起来与 Go 标准库生成的字符串相同:
你确定你真的在点击 Flask 应用程序吗?
我正在尝试编写一个 post 请求函数,将文件从 golang 服务器发送到客户端。代码从这里抄袭了一点 (golang POST data using the Content-Type multipart/form-data)。无论出于何种原因,我总是得到“404 页面未找到”, 即使 通过 postman 完成相同的请求时到达 endpt 并成功返回。这是奇怪的行为,我不太确定如何调试。 (出于测试目的,我已经将 URL 硬编码为命中本地 运行 服务器。)
这是我的代码:
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
}
这是控制台中的输出:
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
有人看到哪里出了问题吗?
编辑:
有人建议将 req 上的转储注释掉,因为它可能会消耗 req 而将请求留空。我试过了,结果和上面一样。
有人提到他们想查看上传成功的 curl 请求,所以这里是:
patientplatypus:~/Downloads:15:48:28$curl -X POST -F "file=@./catpic.jpg" http://127.0.0.1:5000/upload
file uploaded successfully!
带有 -v 标志的 curl 的完整转储:
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!
有人提到他们想看看我试图攻击的服务器,所以这里是:https://github.com/patientplatypus/pythonServer
从表面上看,您没有提出相同的请求。 Curl(感谢包括在内)设置一个表单值,而你的 go 设置 2。在你的 curl 中它是 'file' 但在 go 中,'filename'。我在输出的内容配置行中看到了这一点。使用 -v 卷曲还可能包含有关它发送 f 的内容的更多信息。
该 404 错误字符串看起来与 Go 标准库生成的字符串相同:
你确定你真的在点击 Flask 应用程序吗?