Obj-C POST JSON (body?)到端点问题



在我的Mac上使用Postman,我已经确认GET/POST工作到我的端点。在我的iPad上,我试图做同样的事情,但只有GET连接和返回数据(只是为了测试).

在邮差我有钥匙的设备和价值的[{"name"1","values": [121182243]}]从邮差我可以发送和物理对象响应,服务器返回所有设备的数组,没有别的(我不需要,但这就是它的运行方式)。Postman在Body下有x-www-form-urlencoded。

从我的iPad下面的代码总是返回一个错误400,我认为这意味着我提供了一些服务器不期望的东西。

+(void)makeRequest {
NSError *error = nil;
storedURL = [[NSUserDefaults standardUserDefaults] objectForKey:@"eb-ipaddress-saved"];
NSString *urlstring = [NSString stringWithFormat:@"http://%@",storedURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlstring] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSString *jsonPostBody = @"{"devices":[{"name":"1","values":[121,182,243]}]}";
NSData *postData = [jsonPostBody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if(httpResponse.statusCode == 200)
{
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"The response is - %@",responseDictionary);
NSInteger success = [[responseDictionary objectForKey:@"success"] integerValue];
if(success == 1)
{
NSLog(@"SUCCESS");
}
else
{
NSLog(@"FAILURE");
}
}
else
{
NSLog(@"Error");
}
}];
[dataTask resume];

}

我的4个日志从上面(删除清晰)返回:urlstring http://192.168.90.55:3000 dmx/组jsonPostBody{"devices"[{"name"1","values": [121182243]}]}httpResponse 400误差

我最终会在生产环境中切换到POST中的变量。

谢谢

多亏了Larme的回答,我使用了Postman的代码,并且成功了。

+(void)makeRequest 
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.90.55:3000/dmx/set"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"devices=[{"name":"1","values":[121,182,243]}]" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}

最新更新