$ http.post()方法正在发送GET

dop*_*man 10 http go angularjs angular-http

注意:

我发现了一个可能相关的问题,需要在这里提出一个新问题


这是一个奇怪的问题.我在2年的时间里一直使用角度,从来没有遇到过这个问题.

我正在使用angular v1.5.0.我正在发布这样的帖子请求:

$http({
    method: "POST",
    url: "/myurl",
    data: {
        file: myFile // This is just an object
    }
});
Run Code Online (Sandbox Code Playgroud)

普通的POST请求对吗?得到这个.我查看控制台,"网络"选项卡将请求记录为GET.离奇.所以我已经开始像这样工作:

$http.post("/myurl", {file: myFile});
Run Code Online (Sandbox Code Playgroud)

一样.单步执行$http服务代码后,我确信正在正确设置标头.有没有其他人遇到这个问题?

更新

考虑到德国人的建议,我尝试使用该$resource服务:

promise = $resource("/upload").save()
Run Code Online (Sandbox Code Playgroud)

(由于其他原因,这会返回错误,它仍然可以正确执行POST).我遇到了同样的问题:请求在控制台中记录为GET.

以下是请求到达我的服务器时的标头:

GET /myurl/ HTTP/1.1
Host: localhost:8001
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cache-Control: no-cache
Connection: keep-alive
Pragma: no-cache
Referer: http://localhost:8001/myurl/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
Run Code Online (Sandbox Code Playgroud)

更新2

根据georgeawg的建议,我使用拦截器在各个阶段记录请求.这是拦截器代码:

$httpProvider.interceptors.push(function() {
    return {
        request: function(config) {
            console.log(config);
            return config;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

运行此代码后,我记录了这个:

data:Object // contains file object
headers: Object // has Content-Type set to multipart
method:"POST" // ???
url :"/myurl
Run Code Online (Sandbox Code Playgroud)

所以这意味着请求将从Angular中作为POST发送,但它仍然在浏览器和我的服务器上记录为GET.我认为这里有一些关于HTTP协议的低级别我不明白.

请求是否在实际登录浏览器之前发送到服务器?如果是这样,那至少可以指向我的服务器作为罪魁祸首.

为了找出最新情况,这是我的服务器代码:

type FormStruct struct {
    Test string
}

func PHandler(w http.ResponseWriter, r *http.Request) {
    var t FormStruct

    req, _ := httputil.DumpRequest(r, true)

    log.Println(string(req))
    log.Println(r.Method) // GET
    log.Println(r.Body)

    decoder := json.NewDecoder(r.Body)
    err := decoder.Decode(&t)
    log.Println("Decoding complete")
    if err != nil {
        log.Println("Error")
        panic(err.Error()+"\n\n")
    }
    log.Println(t.Test)

    w.Write([]byte("Upload complete, no errors"))
}

func main() {
    http.HandleFunc("/myurl/", PHandler)    
    fmt.Println("Go Server listening on port 8001")
    http.ListenAndServe(":8001", nil)
}
Run Code Online (Sandbox Code Playgroud)

我的服务器在收到请求时抛出EOF错误:

2016/03/30 10:51:37 http: panic serving [::1]:52039: EOF
Run Code Online (Sandbox Code Playgroud)

不确定EOF在这种情况下甚至意味着什么.

更新3

通过另一种用法的建议,我尝试使用POSTMAN以假的POST请求命中我的服务器.服务器正确接收请求.这对我来说意味着有什么角度来发出POST请求.请帮忙.

有任何想法吗?

完整服务器日志:

Go Server listening on port 8001

2016/03/30 11:13:08 GET /myurl/ HTTP/1.1
Host: localhost:8001
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cache-Control: no-cache
Connection: keep-alive
Content-Type: application/json
Postman-Token: 33d3de90-907e-4350-c703-6c57a4ce4ac0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
X-Xsrf-Token: null


2016/03/30 11:13:08 GET
2016/03/30 11:13:08 {}
2016/03/30 11:13:08 Decoding complete
2016/03/30 11:13:08 Error
2016/03/30 11:13:08 http: panic serving [::1]:52228: EOF


goroutine 5 [running]:
net/http.(*conn).serve.func1(0xc820016180)
    /usr/local/Cellar/go/1.6/libexec/src/net/http/server.go:1389 +0xc1
panic(0x3168c0, 0xc82000b1a0)
    /usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:426 +0x4e9
routes.FUPHandler(0x1055870, 0xc820061ee0, 0xc820104000)
    /Users/projectpath/routes.go:42 +0x695
net/http.HandlerFunc.ServeHTTP(0x4e7e20, 0x1055870, 0xc820061ee0, 0xc820104000)
    /usr/local/Cellar/go/1.6/libexec/src/net/http/server.go:1618 +0x3a
net/http.(*ServeMux).ServeHTTP(0xc820014b40, 0x1055870, 0xc820061ee0, 0xc820104000)
    /usr/local/Cellar/go/1.6/libexec/src/net/http/server.go:1910 +0x17d
net/http.serverHandler.ServeHTTP(0xc820016100, 0x1055870, 0xc820061ee0, 0xc820104000)
    /usr/local/Cellar/go/1.6/libexec/src/net/http/server.go:2081 +0x19e
net/http.(*conn).serve(0xc820016180)
    /usr/local/Cellar/go/1.6/libexec/src/net/http/server.go:1472 +0xf2e
created by net/http.(*Server).Serve
    /usr/local/Cellar/go/1.6/libexec/src/net/http/server.go:2137 +0x44e
Run Code Online (Sandbox Code Playgroud)

更新4

我偶然发现了一些有趣的事情:

我发帖时myurl,Charles记录了一个POST请求,但响应状态为301.在POST之后记录了一个GET.这是击中我的服务器的GET.

如上所示,我的服务器不进行任何重定向.301如何发生?

icz*_*cza 10

这是出于安全考虑.

在您从服务器向浏览器发送重定向的情况下,浏览器不会重复POST请求(而只是"简单"的GET请求).

一般来说,浏览器不会将POST数据发送到重定向URL,因为浏览器没有资格决定您是否愿意将相同的数据发送到您要发送到原始URL的新URL(考虑密码,信用卡)卡号和其他敏感数据).但是不要试图绕过它,只需使用你的处理程序的注册路径来POST,或链接的答案中提到的任何其他提示.

上下文请参阅问题:

Go Web服务器自动重定向POST请求

你可以在这里阅读更多关于这个主题:

为什么HTTP没有POST重定向?