它是我第一次在堆棧溢出中,所以很好: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