OBJ-C POST JSON(正文?)到端点问题
使用Mac上的Postman,我已确认GET/POST工作到我的端点。在我的iPad上,我试图做同样的事情,但只能获得连接和返回数据(仅适用于 测试)。
在Postman中,我有设备的关键和的值[{“ name”:“ 1”,“值”:[121,182,243]}] 从邮递员我可以发送,物理对象响应,服务器返回所有设备的数组,而没有其他设备(我不需要,但这就是它的发展)。 Postman确实在身体下面具有X-WWW形式的纯编码。这是邮递员预期的。
从我的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 JSONPOSTBODY {“ deception”:[{“ name”:“ 1”,“ values”:[121,182,243]}]}} httpresponse 400 错误,
我最终将在生产时切换到我的帖子中的变量。
谢谢
Using Postman on my Mac I have confirmed GET/POST works to my endpoint. On my iPad I am trying to do the same thing but only GET connects and returns data (just for
testing).
In Postman I have key of devices and value of [{"name":"1","values":[121,182,243]}]
From Postman I can Send and the physical object responds and the server returns an array of all devices and nothing else (which I do not require but that is how it goes). Postman does have x-www-form-urlencoded under Body. This works as expected from Postman.
From my iPad the following code always returns an error 400 which I think means I am providing something the server is not expecting.
+(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];
}
My 4 logs from above (removed for clarity) return:
urlstring http://192.168.90.55:3000/dmx/set
jsonPostBody {"devices":[{"name":"1","values":[121,182,243]}]}
httpResponse 400
Error
I will eventually switch to variables in my POST when in production.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多亏了Larme的答案,我使用了Postman的代码,这起作用了。
Thanks to Larme's answer I used the code from Postman and that worked.