如何在IOS中调用PUT请求并需要传递Dictionary



这是我的json字典

{"pid":"14982","type":"intervention","uid":"10008","bookmark_g7l03":{"und":[{"value":"S:1","format":"null","safe_value":"S:1"}]}}

我需要将PUT请求传递到以下URL

http://example.com/services/profiles/pid

让我们知道如何在IOS 中将字典传递到Web服务URL

NSString *data = [NSString stringWithFormat:@"{"pid":"%@","type":"%@","uid":"%@","%@":{"und":[{"value":"%@","format":"null","safe_value":"%@"}]}}",pid,type, uidNo,bkMarkStr,self.startString,self.startString];
NSURL *bkMrkUrl = [NSURL URLWithString:@"http://example.com/services/profiles/pid=14997"];
NSData *postData = [data dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *bkMrkReq = [[NSMutableURLRequest alloc]initWithURL:bkMrkUrl];
[bkMrkReq setHTTPMethod:@"PUT"];
[bkMrkReq setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[bkMrkReq setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:bkMrkReq queue:[NSOperationQueue currentQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

                               NSString *txt = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
                               NSLog(@"data....:%@",txt);
                               // handle response here
                           }];

这里,当我打印文本数据

输出为:<?xml version="1.0" encoding="utf-8"?> <result>CSRF validation failed</result>

我该如何处理这些数据。。我正在更新服务器中的字段信息。

您可以尝试:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://example.com/services/profiles/pid"]];
[request setHTTPMethod:@"PUT"];
NSString *params = @"{"pid" : "14997", "type" : "intervention", "uid" : "10046"}"; // The rest of your parmas here
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                           // handle response here
                       }];

如果你想同步发出请求,你可以使用:

NSURLResponse *response;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

最新更新