当我尝试通过NsMutableUrlRequest将网址发布到Parse.com时,我收到的回复是 代码= 107; error =" json无效:&#34 ;;
函数是用javaScript编写的
但是当我从卷曲发布时,我得到了确切的输出。
curl -X POST \
-H "X-Parse-Application-Id: appID" \
-H "X-Parse-REST-API-Key: restAPiKEy" \
-H "Content-Type: application/json" \
-d '{}' \
https://api.parse.com/1/functions/hello
但是在目标c中我得到了错误 这是我的代码
NSURL *URL = [NSURL URLWithString:@"https://api.parse.com/1/functions/hello"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"POST";
NSString * params = @"----the app id---=X-Parse-Application-Id&-----the rest api key=X-Parse-REST-API-Key";
NSData *data = [params dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:@"APPID" forHTTPHeaderField:@"X-Parse-Application-Id"];
[request setValue:@"REST API KEY" forHTTPHeaderField:@"X-Parse-REST-API-Key"];
[request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Peform the request
NSURLResponse *response;
NSError *error = nil;
NSData *receivedData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
NSDictionary *dictOfNews = [NSJSONSerialization
JSONObjectWithData:receivedData //1
options:kNilOptions
error:&error];
NSLog(@"dict of the news: %@", dictOfNews);
if (error) {
// Deal with your error
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSLog(@"HTTP Error: %ld %@", (long)httpResponse.statusCode, error);
return;
}
NSLog(@"Error %@", error);
}
else
{
NSLog(@"SUCCESS GRAND SUCCESS");
}
});
请告诉我,我哪里出错了。提前致谢