Afnetworking - 后JSON - > 400错误的请求

问题描述:

进出口使用Salesforce的现场代表REST API在iOS应用,试图请求聊天,但总是失败,400的请求是无效ChasitorInitAfnetworking - 后JSON - > 400错误的请求

NSDictionary *parameters [email protected]{  @"SessionId"   :self.sessionId, 
           @"OrganizationId"  :ORG_ID, 
           @"deploymentId"  :DEPLOYEMENT_ID, 
           @"buttonId"   :BUTTON_ID, 
           @"userAgent"   :USER_AGENT, 
           @"language"   :LANG, 
           @"screenResolution" :SCREEN_RES, 
           @"visitorName"  :@"Test Visitor", 
           @"prechatDetails"  :@[], 
           @"prechatEntities" :@[], 
           @"receiveQueueUpdates":@"true", 
           @"isPost"    :@"true" 
          }; 

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 

AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer]; 
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
manager.requestSerializer = serializer; 

[manager.requestSerializer setValue:self.sessionKey forHTTPHeaderField:X_LIVEAGENT_SESSION_KEY]; 
[manager.requestSerializer setValue:@"null" forHTTPHeaderField:X_LIVEAGENT_AFFINITY]; 
[manager.requestSerializer setValue:@"1" forHTTPHeaderField:X_LIVEAGENT_SEQUENCE]; 
[manager.requestSerializer setValue:API_V forHTTPHeaderField:X_LIVEAGENT_API_VERSION]; 

manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
[manager POST:ChasitorInit_path parameters:parameters progress:nil 
     success:^(NSURLSessionDataTask *task, id responseObject) { 
      NSError* error; 
      NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject 
                   options:kNilOptions 
                   error:&error]; 
     } failure:^(NSURLSessionDataTask *task, NSError *error) { 

      NSLog(@"Error: %@", error); 
     }]; 

进出口使用Afnetworking V3 iOS8上 请求体应该是

{ 
"sessionId": "8f1cfb18-fbd1-4487-90d-e98c0a84e165", 
"organizationId": "00D58000000OwMB", 
"deploymentId": "57258000000L1mj", 
"buttonId": "57358000000L24F", 

"userAgent": "", 
"language": "en-US", 
"screenResolution": "1900x1080", 
"visitorName": "Test Visitor", 
"prechatDetails": [], 
"prechatEntities": [], 
"receiveQueueUpdates": true, 
"isPost": true 

}

我做什么我错了? PLZ纠正我!

400错误的请求意味着,您没有按照服务器预期的方式形成请求。

检查服务器日志什么问题...

可能需要另一种形式的数据..

,并尝试检查,你是不是传递空值到服务器..

尝试这样的:

NSString *Loginurl = [NSString stringWithFormat:@"http://Enter your URL Here"]; 

    NSDictionary *params = @{  @"SessionId"   :self.sessionId, 
           @"OrganizationId"  :ORG_ID, 
           @"deploymentId"  :DEPLOYEMENT_ID, 
           @"buttonId"   :BUTTON_ID, 
           @"userAgent"   :USER_AGENT, 
           @"language"   :LANG, 
           @"screenResolution" :SCREEN_RES, 
           @"visitorName"  :@"Test Visitor", 
           @"prechatDetails"  :@{}, 
           @"prechatEntities" :@{}, 
           @"receiveQueueUpdates":@"true", 
           @"isPost"    :@"true" 
          }; 

    NSLog(@"Sent parameter to server : %@",params); 

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 



    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments]; 


    manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 


    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]; 

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",nil]; 

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",nil]; 

[manager.requestSerializer setValue:self.sessionKey forHTTPHeaderField:X_LIVEAGENT_SESSION_KEY]; 
[manager.requestSerializer setValue:@"null" forHTTPHeaderField:X_LIVEAGENT_AFFINITY]; 
[manager.requestSerializer setValue:@"1" forHTTPHeaderField:X_LIVEAGENT_SEQUENCE]; 
[manager.requestSerializer setValue:API_V forHTTPHeaderField:X_LIVEAGENT_API_VERSION]; 

      AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate]; 

    [policy setValidatesDomainName:NO]; 

    [policy setAllowInvalidCertificates:YES]; 



    [manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) { 

     NSLog(@"Response from server : %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); 



     _str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; 


     NSLog(@"%@",_str); 


     [self getdata:responseObject]; 



    } failure:^(NSURLSessionTask *operation, NSError *error) 

    { 

     NSLog(@"Error: %@", error); 
    }];  
} 

#pragma mark-DataFromServer 

-(void)getdata:(NSDictionary*)RegisterData 

{ 


} 
+0

感谢您的答复,我更新的问题,请参见PARAMS生成的JSON – Gilber

+0

更换后有什么错误@ “prechatDetails”:@ {},由@ “prechatDetails”:@ [], –

+0

改变现在出现错误“字符0周围的值无效”。 – Gilber

我在我的应用得到了同样的错误。我通过删除请求和响应串行器来解决它。只实例化管理器并用参数调用post方法。

reqManager = [AFHTTPRequestOperationManager manager]; 
[reqManager POST:URLString parameters:parameters progress:nil 
     success:^(NSURLSessionDataTask *task, id responseObject) { 

     } failure:^(NSURLSessionDataTask *task, NSError *error) { 

      NSLog(@"Error: %@", error); 
     }];