0

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);
}
Asd_123
  • 53
  • 8
  • Why do you add () ? return request; –  Feb 15 '17 at 05:09
  • Check if this has value `[NSURL URLWithString:temp]` – Rajesh Feb 15 '17 at 05:11
  • @iDev I think he checks the URL in the 'NSLog(@"%@",temp);' anad I think we need the rest of the code of how he calls this method and where he checks that it is "null" to know what he does with it –  Feb 15 '17 at 05:13
  • @sneak i put break point before return expression and print the 'request' . there it shows null. but nslog shows the url. – Asd_123 Feb 15 '17 at 05:24
  • Probably URL would be invalid, want to see o/p of `NSLog(@"%@",temp);` – Anil Varghese Feb 15 '17 at 05:33
  • @AlwinVarghese Yes, temp will not nil but it is possible that `[NSURL URLWithString:temp]` will be nil, try to log `[NSURL URLWithString:temp]` and check is it nil or not. – Nirav D Feb 15 '17 at 05:37
  • No. URL is valid. i have checked in REST Client. I Want to know is this the correct method for sending NSMutableURLRequest @Anil Varghese – Asd_123 Feb 15 '17 at 05:38
  • To use the `[NSURL URLWithString:temp]`. The temp string should start with `http://` or `https://`. – Sachin Vas Feb 15 '17 at 05:45
  • @AlwinVarghese Have you try once checking with `NSLog(@"%@",[NSURL URLWithString:temp])`, and check in console is it printing url or nil in the console. – Nirav D Feb 15 '17 at 05:51
  • Yah that's i checked. Log is null @Nirav D – Asd_123 Feb 15 '17 at 05:53
  • @AlwinVarghese So that is the reason you are getting null with request. Can you edit with dummy example url that you are trying, – Nirav D Feb 15 '17 at 05:54
  • @AlwinVarghese Have you not read my answer I've posted for you? I even made a big EDIT on it to make it more clear. –  Feb 15 '17 at 05:55
  • @Sneak i checked your answer. thats working. i think the issue may on web service. But still getting web service using in REST Client. – Asd_123 Feb 15 '17 at 06:00
  • 1
    @AlwinVarghese Not sure what you mean by "still geting web service using REST client". If you mean that you can not connect to your REST, make sure you have HTTPS (not HTTP) or if you only have HTTP, you must add the domain to your .plist for iOS to not block the connection: See http://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http for this –  Feb 15 '17 at 06:03
  • @AlwinVarghese There is something wrong in constructing the `temp` url string. Show us the log, you can mask part of base url if you are concerned about confidentiality. We want to see the format – Anil Varghese Feb 15 '17 at 06:05
  • Yah thats Worked. after adding the NSAllowsArbitraryLoads into Plist. Thanks @Snake and supporters – Asd_123 Feb 15 '17 at 06:39

1 Answers1

1

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.

EDIT:

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.

Community
  • 1
  • 1