我有这个代码:
request.HTTPMethod = @"POST";
NSString *postString = [@"content=" stringByAppendingString:@"stuff here"];
request.HTTPBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:[[NSNumber numberWithInt:[postString length]] stringValue] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *resp = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
NSLog(@"%@", [[NSString alloc] initWithBytes:[resp bytes] length:[resp length] encoding:NSUTF8StringEncoding]);
我希望发送一个包含内容=一些东西的帖子请求
但相反,服务器的行为就像是得到了一个 GET。怎么了?
这是我的服务器端代码(ruby、sinatra、heroku)
get '/messages' do
@messages = Message.all
erb :messages
end
get '/messages/:id' do
Message.find(params[:id]).content
end
post '/messages' do
@content = Message.new({:content => params[:content]})
RestClient.post API_URL+"/messages",
:from => "idoor@idoor.heroku.com",
:to => "drew.a.gross@gmail.com",
:subject => "New Message from iDoor!",
:html => @content.content
@content.save
@content.content
end
这是服务器上的日志文件。我不明白到底发生了什么,看起来有一个 POST 和一个 GET
2013-09-16T06:41:37.482489+00:00 heroku[router]: at=info code=H16 desc="herokuapp redirect" method=POST path=/messages host=idoor.heroku.com fwd="129.97.131.0, 127.0.0.1" dyno= connect= service= status=301 bytes=
2013-09-16T06:41:37.741972+00:00 app[web.1]: 129.97.131.0 - - [16/Sep/2013 06:41:37] "GET /messages HTTP/1.1" 200 398 0.0850
2013-09-16T06:41:37.747154+00:00 heroku[router]: at=info method=GET path=/messages host=idoor.herokuapp.com fwd="129.97.131.0" dyno=web.1 connect=0ms service=95ms status=200 bytes=398