【问题标题】:Sending POST with objective-C and fetching it with python using flask使用 Objective-C 发送 POST 并使用烧瓶使用 python 获取它
【发布时间】:2014-10-17 10:03:48
【问题描述】:

这是我第一次在这里堆栈溢出,所以很好:P。这是我应该向我的网络服务器发送一些信息的方法。

-(IBAction)updateEvent:(id)sender{

    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];

    [[dict objectForKey:@"name"] addObject:_getNameLabel.text];

    static NSString *url = @"http://localhost:5000/hello";
    NSError *error;
    NSData *event = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    if (! event) {
        NSLog(@"Got an error: %@", error);
    } else {
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:event];
        [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[event length]] forHTTPHeaderField:@"Content-Length"];
        [request setURL:[NSURL URLWithString:url]];
    }


    // [gameInfoObject postEventInfo:event];

}

这是“应该”处理帖子消息的代码。我要做的就是获取该信息并将其写入现有文件。

import pickle
from flask import Flask
import json

@app.route('/hello/' , methods=['POST'])
def test():

    with open("text.txt", "w") as text_file:  #to check that it even gets here but it does not write anything at all to the file
        text_file.write("It came here!!")

    data = request.data
    dataDict = json.loads(data)

    with open('gameFile.txt', 'wb') as handle:
        pickle.dump(dataDict, handle)


if __name__ == "__main__":
    app.run()

所以当我运行它时,我没有得到任何错误,但我也没有得到任何结果。该文件仍然是空的,我在这里缺少什么?感谢我能得到的所有帮助!

【问题讨论】:

  • def test方法的末尾尝试return json.dumps({'msg': 'success'})
  • 我在哪里可以看到该消息? @SyedHabibM
  • 在您的objective-c 代码响应中?!
  • 不,我什么也得不到。我编辑了我的问题,我在 python 代码中放了另一行来检查它是否到达那里,但似乎它没有......@SyedHabibM

标签: python objective-c flask request http-post


【解决方案1】:

您的网址不匹配。

您已在 Flask 中将路由定义为/hello/

@app.route('/hello/' , methods=['POST'])

但是,当您从 Objective-C 发出请求时,您将其发送到 /hello

static NSString *url = @"http://localhost:5000/hello";

当 Flask 收到请求时,它会发送带有 301 状态代码的响应,告诉客户端资源已移动到 /hello/。客户端不遵循重定向。

更新您的路线或更新您的请求,您应该已经准备好了。

编辑:

您也没有发送请求。您发布的代码准备了 POST 请求,但您需要打开一个连接。我对 Objective-C 不是很熟悉,但它应该类似于以下内容。

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request 
                                                            delegate:self];

[connection start]; 

【讨论】:

  • 但在浏览器中要到达那个地方我需要写“localhost:5000/hello”不只是你好? @dirn
  • 如果您在浏览器中转到localhost:5000/hello,它将重定向到localhost:5000/hello/
  • 啊哈,现在我明白了,呵呵。我只是将字符串 url 更改为 @"localhost:5000/hello" 但它仍然不起作用:/ :( @dirn
  • Flask 部分是否单独工作? curl -H "Content-type: application/json" -d "{'key':'value'}" http://127.0.0.1:5000/hello/(或 Object-C 设置的任何内容类型)
  • 是的,它可以工作,我启动 Web 服务器,然后在我的 shell 中写下你在此处写的内容并将其写入文件。所以我猜我没有在目标c中发送正确的方式? @dirn
猜你喜欢
  • 2020-05-14
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 2013-04-03
  • 2017-06-27
  • 1970-01-01
相关资源
最近更新 更多