Ios 如何将字符串数据发布到API
我想将字符串数据发布到API中,我尝试使用下面的代码将其发送到服务器。我已经使用postman检查了api,它没有将字符串数据传递到服务器。我不知道是什么问题,需要帮助Ios 如何将字符串数据发布到API,ios,objective-c,iphone,xcode,Ios,Objective C,Iphone,Xcode,我想将字符串数据发布到API中,我尝试使用下面的代码将其发送到服务器。我已经使用postman检查了api,它没有将字符串数据传递到服务器。我不知道是什么问题,需要帮助 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:reqURLStr]]; [request setHTTPMethod:@"POST"]; **//Pass The String t
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:reqURLStr]];
[request setHTTPMethod:@"POST"];
**//Pass The String to server**
NSString *userUpdate =[NSString stringWithFormat:@"service_type=%@&ParcelSize=%@&ReceiverName=%@&MobileNumber=%@&Email=%@&DropOffHub=%@&PickupHub=%@" ,serviceType,pSize,rName,rMobile,rEmail,dropHubID,pickHubID];
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data1];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[data makeRestAPICall:reqURLStr] forHTTPHeaderField:@"Authorization"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
NSLog(@"got response==%@", resSrt);
if(resSrt)
{
NSLog(@"got response");
}
else
{
NSLog(@"fail to connect");
}
return resSrt;
简单回答
-(void)postJsonDataToServer{
NSDictionary *parameters = @{
@"service_type": serviceType,
@"ParcelSize": pSize,
@"ReceiverName": rName,
@"MobileNumber": rMobile,
@"Email" : rEmail,
@"DropOffHub" : dropHubID,
@"PickupHub" : pickHubID
};
NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http:/api/order/add"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *dataTask = [session uploadTaskWithRequest: request
fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(data != nil)
{
NSError *parseError = nil;
//If the response is in dictionary format
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
//OR
//If the response is in array format
NSArray *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"The res is - %@",res);
}
else
NSLog(@"Data returned the parameter is nil here");
}];
[dataTask resume];
}
简单回答
-(void)postJsonDataToServer{
NSDictionary *parameters = @{
@"service_type": serviceType,
@"ParcelSize": pSize,
@"ReceiverName": rName,
@"MobileNumber": rMobile,
@"Email" : rEmail,
@"DropOffHub" : dropHubID,
@"PickupHub" : pickHubID
};
NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http:/api/order/add"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *dataTask = [session uploadTaskWithRequest: request
fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(data != nil)
{
NSError *parseError = nil;
//If the response is in dictionary format
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
//OR
//If the response is in array format
NSArray *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"The res is - %@",res);
}
else
NSLog(@"Data returned the parameter is nil here");
}];
[dataTask resume];
}
从响应中得到了什么错误?{状态代码:400,标题{“缓存控制”=“无缓存”;“内容长度”=0;日期=“Sat,2017年6月3日06:37:33 GMT”;Expires=“-1”;Pragma=“无缓存”;Server=“Microsoft IIS/7.5”;“X-AspNet-Version”=“4.0.30319”;“X-Powered-By”=“ASP.NET”;}I“po响应”它向我显示了这个你能分享你的请求URL吗?这样我就可以查看metterNSString*apiURL=@“http:/api/order/add”;[self-makeRestAPICall:apiURL];你从响应中得到了什么错误?{状态代码:400,标题{“缓存控制”=“无缓存”;“内容长度”=0;日期=“2017年6月3日周六06:37:33 GMT”;过期=“-1”Pragma=“no cache”;Server=“Microsoft IIS/7.5”;“X-AspNet-Version”=“4.0.30319”;“X-Powered-By”=“ASP.NET”;}I“po respond”并显示此信息。您可以共享您的请求URL吗?这样我就可以查看metternstring*apiURL=@“http:/api/order/add”;[自制餐厅:apiURL];欢迎兄弟会