Murali
Murali

Reputation: 49

send JSON Object Request to Server

Iam sending JSON Object request to the server but server returns Status Code 405. how to solve this problem. please any one help me. My code :

+(NSData *)GpBySalesDetailed:(NSMutableDictionary *)spDetailedDict{

NSLog(@"spDetailedDict:%@",spDetailedDict);
NSString *dataString = [spDetailedDict JSONRepresentation];
NSLog(@"%@dataString",dataString);

return [dataString dataUsingEncoding:NSUTF8StringEncoding];
}
-(void)requestWithUrl:(NSURL *)url WithJsonData:(NSData *)JsonData
{
   NSMutableURLRequest *urlRequest=[[NSMutableURLRequest alloc]initWithURL:@"http://srbisolutions.com/SmartReportService.svc/GpBySalesPersonDetailed];
if (JsonData != nil) {
    [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:JsonData];
}
else
{
    [urlRequest setHTTPMethod:@"GET"];  
}
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
[conn start];
}

Upvotes: 0

Views: 1165

Answers (3)

Kasaname
Kasaname

Reputation: 1501

 NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"yourURL"]];


            [theRequest setHTTPMethod:@"POST"];

            NSDictionary *jsonRequest =
            [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@//add your objects
                                                 ]
                                        forKeys:[NSArray arrayWithObjects:
                                                 title,
                                                 link,
                                                 nil]];

            NString *jsonBody = [jsonRequest JSONRepresentation];
            NSLog(@"The request is %@",jsonBody);

            NSData *bodyData = [jsonBody dataUsingEncoding:NSUTF8StringEncoding];
            [theRequest setHTTPBody:bodyData];

            [theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
            // create the connection with the request

            // and start loading the data

            theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

Upvotes: 0

Satish Azad
Satish Azad

Reputation: 2312

try this

 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:YOURURL  
                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0 ];

        NSLog(@"final request is %@",request);


        [request setHTTPMethod:@"POST"];

        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

     //Here postData is a Dictionary with key values in web services format  use ur own dic

        [request setHTTPBody:[[self convertToJSON:postData] dataUsingEncoding:NSUTF8StringEncoding]];  


        NSString    *contentLength = [NSString stringWithFormat:@"%d",[[request HTTPBody] length]]; 
        [request setValue:contentLength forHTTPHeaderField:@"Content-Length"];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

        if (connection) 
        {
            self.responseData = [NSMutableData data];
        }






//============JSON CONVERSION========
-(NSString *)convertToJSON:(id)requestParameters
{
    NSData *jsonData   = [NSJSONSerialization dataWithJSONObject:requestParameters options:NSJSONWritingPrettyPrinted error:nil];

    NSLog(@"JSON DATA LENGTH = %d", [jsonData length]);

    NSString *jsonString    = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSLog(@"JSON STR LENGTH = %d", [jsonString length]);

    return jsonString;

}

Upvotes: 0

vikingosegundo
vikingosegundo

Reputation: 52227

HTTP Code 405 means "Method not allowed", it does not accept a post request for this particular URI. Either the server must be configured to accept POST requests or it should offer another URI.

Upvotes: 1

Related Questions