- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试编写一个 post 请求函数,将文件从 golang 服务器发送到客户端。代码从这里(golang POST data using the Content-Type multipart/form-data)抄了一点。无论出于何种原因,我总是收到“404 页面未找到”,即使通过 postman 完成的相同请求正在到达终点并成功返回。这是奇怪的行为,我不太确定如何调试。 (出于测试目的,我已经对 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
最佳答案
那个 404 错误字符串看起来和 Go 标准库生成的一样:
你确定你真的在点击 Flask 应用程序吗?
关于Golang http.NewRequest 总是返回 404 - Postman ok,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54382921/
我正在使用以下代码读取我在文本编辑器 (Notepad++) 中创建的文本 (.xml) 文件,将我从中读取的 UTF-8 文本转换为 UTF-16,以便 Windows API 函数可以使用它,然后
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
我知道所有的论坛都充满了这样的问题,但我尝试了几个钩子(Hook),但它们不起作用(或者我做的不好)。 所以,我有: main.cpp <- fawn.h <- connector.cpp (defe
这是我正在使用的一段代码。 当 items 为 null 时,ok(Object items) 方法在内部调用 Jersey 的 Response.ok() 方法。 MembershipReq
我在 Tcl/Tk 中有一个简单的窗口,点击确定按钮运行模拟。我正在使用 Linux。模拟完成后,窗口将被销毁。问题是在模拟运行时窗口仍然存在。我希望窗口在我点击确定按钮后消失。 我尝试使用 wm w
在我们的网络应用程序中,我将 Angular-Materials $mdDialog 与确认对象一起使用。是否可以将按钮的顺序从取消-确定更改为确定-取消?并将初始焦点设置为取消按钮?也许通过 CSS
这个问题在这里已经有了答案: 关闭10年前。 Possible Duplicate: Ternary conditional operator in Python 我有这个问题,不知道要问谷歌: (v
我尝试使用 R 进行回归。我有以下代码,导入 CSV 文件没有问题 dat <- read.csv('http://pastebin.com/raw.php?i=EWsLjKNN',sep="
在 QInputDialog 中,如何去掉 OK 和 Cancel 按钮中的图标? 注意取消和确定的图标。我查看了属性按钮,不知道如何删除它们。 最佳答案 解决方案的策略是先获取按钮,但是这些属于QD
当使用Postman测试项目时,任何POST方法,我收到的是200 OK而不是201 Created,并且subreddit不是在数据库中创建的,并且在控制台休眠中接收到以下内容:SELECT T1_
当使用Postman测试项目时,任何POST方法,我收到的是200 OK而不是201 Created,并且subreddit不是在数据库中创建的,并且在控制台休眠中接收到以下内容:SELECT T1_
我制作了一个安卓应用程序,可以从本地 wifi 网络传输语音。为了收听和流式传输,我使用 JNI 中的 Opus C API 进行解码,并使用 OpenSL Audio 进行读取。 我从 Servic
我有一个定义如下的 map : mapMeasures := make(map[time.Time]models.Measure, 0) 与 type Measure struct { Del
这里我的数据集是 pd我已将其拆分为训练和测试数据 pd_train1和 pd_train2 sku national_inv lead_time in_transit_qty forecas
我已经检查过有关此问题的其他问题,但由于问题似乎非常具体,因此它们没有帮助。 我有一个像这样的数据框(这只是一个简单的示例,下面提供了来自 dput() 的示例数据): year species ab
当我使用 anova_test() 函数(来自 rstatix 包)做双向重复测量方差分析时,出现错误: lm.fit(x, y, offset = offset, singular.ok = sin
我一直在尝试对数据集进行 2-Way 重复测量测试,年份和疫苗类型是自变量,覆盖率是因变量。我用代码运行它: sat = anova_test( data=SA, dv = coverage, w
奇遇 我是一个普通的大学生,尹成是我的名字。顾名思义,我和其他人一样,没有什么特别之处。然而,在某个偶然的机会下,我发现了一个全新的领域——编程。 初印象 说实话,我对编程并不了解,甚至可以说是一窍不
我使用这些代码创建了一个卡拉 OK 并将其刻录到 VCD。 xxxxxx_1.m4a 文件是左声道(乐器),xxxxxx_0.m4a 文件是右声道(带人声的歌曲)。 将它们组合到 become xxx
我不确定为什么会发生这种情况,但我有一个简单的 Ajax 代码: $.ajax({ url: "/javascript/testing.js"}) .done(function(data){
我是一名优秀的程序员,十分优秀!