Reputation: 53
I have a string with webservice.But when i add the string into NSMutableRequest, The method return become null.Here is my code,
-(NSMutableURLRequest *)createRequest:(NSString *)convertedString{
NSString *temp=[NSString stringWithFormat:@"%@%@",Api_Link,convertedString];
NSLog(@"%@",temp);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:temp]];
[request setHTTPBody:[convertedString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"App IPHONE" forHTTPHeaderField:@"USER_AGENT"];
[request setHTTPMethod:@"POST"];
return(request);
}
Upvotes: 0
Views: 253
Reputation:
I ran your code in an Xcode Project:
NSString *temp = @"https://www.google.se/";
NSString *temsssp = @"https://www.google.se/";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:temp]];
[request setHTTPBody:[temsssp dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"App IPHONE" forHTTPHeaderField:@"USER_AGENT"];
[request setHTTPMethod:@"POST"];
NSLog(@"%@", request);
Result:
2017-02-15 06:43:23.061816 Sneak[5023:3002603] <NSMutableURLRequest: 0x170218120> { URL: https://www.google.se/ }
So there is nothing wrong with your request if you have a valid URL.
If you need further details and help, provide further information and code on how you run this method, and I will update the answer accordingly as your question changes or is edited.
As I mentioned, you need a Valid URL, I did the NSLog for you with this line:
NSString *temp = @"somethingsomething";
And the results are nil. So again, you need a valid URL.
Upvotes: 1