Asd_123
Asd_123

Reputation: 53

NSMutableUrlRequest returning null value

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

Answers (1)

user5890979
user5890979

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.

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.

Upvotes: 1

Related Questions