它是我第一次在堆栈溢出中,所以很好:P。这是我的方法,应该发送一些信息到我的网络服务器。用Objective-C发送POST并使用python使用python获取POST
-(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'})''。 – 2014-10-17 10:08:25
我在哪里可以看到该消息? @SyedHabibM – 2014-10-17 10:09:44
在你的'objective-c'代码响应中? – 2014-10-17 10:12:23