gpt4 book ai didi

ios - 如何在IOS中调用PUT请求需要传递一个Dictionary

转载 作者:行者123 更新时间:2023-11-29 02:11:22 26 4
gpt4 key购买 nike

这是我的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 中将字典传递给 Webservice 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];

关于ios - 如何在IOS中调用PUT请求需要传递一个Dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29251046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com