将Curl命令转换为Objective C

时间:2015-11-16 21:02:21

标签: objective-c curl

以下卷曲线在终端工作,但我无法弄清楚如何正确地将其翻译成NSURL

curl -X PUT http://user:pass@localhost:3000/update/564a3f1ff72fe641a8000013/? -d firstName=Adam

这是我到目前为止摇摆的代码并且它连接但是创建了值为NIL的字段而不是用新值来创建它们

NSString *authString = [NSString stringWithFormat:@"%@:%@%c",@"user", @"pass", '@'];

NSData *postData = [@"firstName=Adam" dataUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@localhost:3000/update/564a3f1ff72fe641a8000013/?",authString]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"PUT"];
[req setHTTPBody:postData];

NSError *err = nil;
NSHTTPURLResponse *res = nil;
NSData *retData = [NSURLConnection sendSynchronousRequest:req returningResponse:&res error:&err];
if (err)
{
    //handle error
    NSLog(@"BOOO : %@",err);
}
else
{
    //handle response and returning data
    NSLog(@"YAYA : %@",err);
}

1 个答案:

答案 0 :(得分:2)

设置您的请求的内容类型:

[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];