当我向REST api发出POST请求时出现错误

时间:2015-06-12 08:37:20

标签: ios xcode rest

我想在 REST api 中发送 POST 请求,但是当我在服务器端看到发送错误消息时,会收到类似此类型的信息 - 。

**{"messages":{"error":[{"code":404,"message":"Request does not match any route."}]}}**

发生此错误。 我的程序代码在这里 -

NSString *post = [NSString stringWithFormat:@"postBody=%@",@"Rama"];
 NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]];

 NSURL *myURL = [NSURL URLWithString:@"http://developer.b24solutions.com/grocery/api/rest/"];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:0.0];

 [request setHTTPMethod:@"POST"];
 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];


[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *clearSession = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[clearSession start];
NSLog(@"%@",clearSession);
if(clearSession){
    NSLog(@"data send");
}
else
{
    NSLog(@"connection failed");
}

所以我哪里出错了。 请帮我。 并在服务器端工作在相同的URL上的PHP。

 <?php
$result = $_POST['postBody'];


echo $result;

&GT;

1 个答案:

答案 0 :(得分:0)

错误代码404表示文件不是fount。您的api链接不是文件名退出。请添加完整的api路径,例如

developer.b24solutions.com/grocery/api/rest/yourfilename

因为此链接在浏览器中不起作用。您的代码是正确的,但不正确的api链接。

req.session.passport.user.id