$ http.post()方法actally发送一个GET [英] $http.post() method is actally sending a GET

查看:315
本文介绍了$ http.post()方法actally发送一个GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现值得一个新的问题<一个一个可能相关的问题href=\"http://stackoverflow.com/questions/36316429/go-web-server-is-automatically-redirecting-post-requests\">here

I've found a possibly related issue that warrants a new question here

这是一个奇怪的问题。我一直在使用的角度超过2年的过程中,从来没有碰到这个问题。

This is a weird problem. I've been using angular over the course of 2 years and have never run into this problem.

我使用的角度V1.5.0。我在做一个POST请求是这样的:

I'm using angular v1.5.0. I'm making a post request like this:

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

运行中的磨POST请求权?得到这个。我看在控制台和网络选项卡记录的请求作为GET。离奇。所以,我jiggered的code这样的工作:

Run-of-the-mill POST request right? Get this. I look in the console and the Network tab logs the request as a GET. Bizarre. So I've jiggered the code to work like this:

$http.post("/myurl", {file: myFile});

同样的事情。通过加强 $ HTTP 服务code后,我确信这个头被设置正确。有没有其他人碰到这个问题?

Same thing. After stepping through the $http service code I'm confident that the headers are being set properly. Has anyone else run into this problem?

germanio 的建议,我使用 $资源已经试过服务,而不是:

Taking germanio's advice, i've tried using the $resource service instead:

promise = $resource("/upload").save()

(这将返回一个错误的另一个原因是,它仍能正常执行POST)。我有同样的问题:请求被记录为在控制台的GET。

(this returns an error for another reason, it still executes the POST properly). I'm having the same problem: the request is logged as a GET in the console.

下面是请求的头当它到达我的服务器:

Here are the headers of the request when it gets to my server:

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

更新2

根据 georgeawg 的建议我使用一个拦截器登录在其各个阶段的要求。这里是拦截code:

Update 2

As per georgeawg's suggestion I've used an interceptor to log the request at its various stages. Here is the interceptor code:

$httpProvider.interceptors.push(function() {
    return {
        request: function(config) {
            console.log(config);
            return config;
        }
    }
}

已经运行此code我得到这样的记录:

Having run this code I get this logged:

data:Object // contains file object
headers: Object // has Content-Type set to multipart
method:"POST" // ???
url :"/myurl

因此​​,这意味着该请求被发送从内部角度帖子,但它仍然记录为无论是在浏览器和我的服务器上GET。我认为这是在工作中一些低水平这里有关我不明白HTTP协议。

So this means the request is being sent as a POST from within Angular, but it is still logged as a GET both in the browser and on my server. I think there is something low level at work here about the HTTP protocol that I dont understand.

时向服务器发送请求之前,在浏览器中实际记录?如果是这样,这可能ATLEAST点到我的服务器的罪魁祸首。

Is the request sent to the server before it is actually logged in the browser? If so, that might atleast point to my server as the culprit.

在找出什么是继续下去的希望,这里是我的服务器code:

In the hopes of finding out whats going on, here is my server code:

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)
}

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

My server throws an EOF error when it receives the request:

2016/03/30 10:51:37 http: panic serving [::1]:52039: EOF

不知道一个EOF甚至会在这方面的意思。

Not sure what an EOF would even mean in this context.

被另一个使用的建议,我尝试使用邮递员打我用假POST请求的服务器。服务器正常接收请求。这意味着,我认为也有一些是与如何角正在POST请求。请帮助。

By the suggestion of another use, I tried using POSTMAN to hit my server with a fake POST request. The server receives the request properly. This means to me that there is something up with how angular is making the POST request. Please help.

任何想法?

全服务器日志:

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

更新4

我无意中发现了一些有趣的事情:

Update 4

I stumbled onto something interesting:

查尔斯日志时将帖子发到 myurl POST请求,但响应的状态是301一个GET登录后门柱。这是打我的服务器上的GET。

Charles logs a POST request when I post to myurl, but the response status is 301. After the POST a GET is logged. This is the GET that is hitting my server.

我的服务器,你可以在上面看到,没有做任何类型的重定向。 301是如何发生的?

My server, as you can see above, does not do any sort of redirection. How is the 301 happening?

推荐答案

这是由于安全考虑。

在当一个重定向从服务器到浏览器发回你的情况,浏览器不会重复POST请求(而只是一个简单的GET请求)。

In your situation when a redirect is sent back from the server to the browser, the browser will not repeat the POST request (but rather just a "simple" GET request).

一般来说浏览器将不会发送POST数据到重定向URL,因为浏览器是没有资格决定,如果你愿意相同的数据发送到新的URL,你打算送什么给原始URL(想想密码,信用卡号码和其它敏感数据)。但是不要试图绕过它,只需使用注册的路径处理程序,才能发布,或者任何链接的回答中提到的其他技巧。

Generally speaking a browser will not send POST data to a redirect URL because the browser is not qualified to decide if you're willing to send the same data to the new URL what you intended to send to the original URL (think about passwords, credit card numbers and other sensitive data). But don't try to circumvent it, simply use registered path of your handler to POST to, or any of the other tips mentioned in the linked answer.

有关背景下看问题:

<一个href=\"http://stackoverflow.com/questions/36316429/go-web-server-is-automatically-redirecting-post-requests\">Go Web服务器自动重定向POST请求

您可以阅读更多关于这个问题在这里:

You can read more on the subject here:

为什么没有HTTP POST有重定向?

这篇关于$ http.post()方法actally发送一个GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆