douxiaomang5640 2014-05-05 01:38
浏览 81
已采纳

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

My server code like:

// 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
    // ...
}

Client side:

// 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;
NSData *inputJson = [NSJSONSerialization dataWithJSONObject:obj options:0 error:&error];
if (error) {
    // no error here
}
[req setHTTPBody:inputJson];

[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    if (error) {
        NSLog(@"ERROR: %@", error.localizedDescription);
        return;
    }

    NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
    NSString *outputText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"response (%d):
%@", (int)resp.statusCode, outputText);
}

When I run this iOS code in iPhone simulator the server side shows:

method: GET
content length: 0

Chage my client side code: [req setHTTPMethod:@"HEAD"];, the server works:

method: HEAD
content length: <some length>

I don't know what's wrong with my POST code on the client or what should I do on the server to retrieve data as []byte my client just sent.

btw, my go version:

$ go version
go version go1.2.1 darwin/amd64
  • 写回答

1条回答 默认 最新

  • donglian4464 2014-05-05 03:03
    关注

    I solved this! So strange behavior. I don't know why this could happen.

    I register my handler like:

    // addr := ...
    r := mux.NewRouter()
    r.Handle("/abc", new(SomeHandler))
    http.Handle("/", r)
    // here mux is github.com/gorilla/mux
    // these lines of code do the same thing as wrote:
    //   http.handle("/abc", new(SomeHandler))
    
    err := http.ListenAndServe(addr, nil)
    // ...
    

    Then my client sends a request to say

    http://addr:9999//abc
    

    But the correct URL should be

    http://addr:9999/abc
    

    The redundant / generated when call -stringByAppendingString: method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值