小编Eri*_*hai的帖子

为什么Golang将我的POST请求视为GET请求?

我的服务器代码如:

// golang
type SomeHandler struct{}

func (*SomeHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    fmt.Println("method:", req.Method)
    fmt.Println("content length:", req.ContentLength)

    // read request body as []byte
    content, err := ioutil.ReadAll(req.Body)
    if err != nil {
        // do sth.
        return
    }

    // decode JSON
    // ...
}
Run Code Online (Sandbox Code Playgroud)

客户端:

// Objective-C on iOS 6/7
// NSURL *myUrl = ...
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:myUrl];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

id obj = @{
             @"username" : @"myname",
             @"password" : @"mypwd",
          };
NSError *error; …
Run Code Online (Sandbox Code Playgroud)

http go ios

2
推荐指数
1
解决办法
674
查看次数

标签 统计

go ×1

http ×1

ios ×1