我有这段代码:
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]);
我希望发送带有content = some stuff
的帖子请求但是服务器的行为就像得到了一个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
答案 0 :(得分:0)
首先,您应该使用百分比编码(不是UTF-8)对帖子正文进行编码。
其次,您是否认为可能是您的服务器做错了什么。你可以发布服务器端代码吗?