开发者

NSURLRequest POST to google app engine?

I am currently looking into GAE (Python) and I'm trying to send POST requests from an iPhone application.

Sending with GET method works fine, but it just fails when I use POST method.

Here's the server side:

class echoHandler(webapp.RequestHandler):
    def get(self):
        password = self.request.get("password")
        if password == "ping":
            self.response.out.write("pong")
        else:
            self.response.out.write("erreur de password")

    def post(self):
        password = self.request.get("password")
        if password == "ping":
            self.response.out.write("pong")
        else:
            self.response.out.write("erreur de password")

and on the client side:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myapp.appspot.com/echo"]];
    NSMutableURLRequest *maRequete = [NSMutableURLRequest requestWithURL:url];

    [maRequete setHTTPMethod:@"POST"];
    NSString *essaiMdp = @"password=ping";
    [maRequete setHTTPBody:[essaiMdp dataUsingEncoding:NSUTF8StringEncoding]];
    //[maRequete setValue:[NSString stringWithFormat:@"%d",[essaiMdp length]] forHTTPHeaderField:@"Content-Length"];
    //[maRequete setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
    //[maRequete setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];

    NSURLResponse *reponse;
    NSError *erreur;

    NSData *donneesPOST = [NSURLConnection sendSynchronousRequest:maRequete returningResponse:&reponse error:&erreur];
开发者_高级运维
    if (donneesPOST) {
        NSString *strResultat = [[NSString alloc] initWithData:donneesPOST encoding:NSUTF8StringEncoding];
        NSLog(@"reponse recue !");
        NSLog(strResultat);
        resultat.text = strResultat;

    }
    else {
        if (erreur) {
            NSLog(@"erreur lors de la requete HTTP:%@", url);
            NSLog(@"erreur: %@", [erreur localizedDescription]);

        }
    }

What's weird is that everything works in localhost, but when I deploy the app on the internet, POST method stopped working.

Any idea ? Thx !


Is there a possibility that your server is set up to redirect requests that don't end in a slash to one that does? In other words, maybe http://myapp.appspot.com/echo is redirecting to http://myapp.appspot.com/echo/, and a redirect is always a GET.


If you are sending a POST request to Google App Engine, change the URL to 'https' "http://myapp.appspot.com/echo" to "https://myapp.appspot.com/echo".

From what i have experienced, sending a "non-https" url to Google App Engine causes a redirect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜