Question

i have code of "PUT" method , please tell me something is wrong in my whole method ...code not affect at server side ....please tell me because i am fresher so i didn't know whats wrong with my code

     -(void)PutMethod:(NSString *)url andPostData:(NSString *)putData
     {
NSLog(@"%@",url);//here get the right url which i want..
NSLog(@"%@",putData);//here get right data which i want to upload...

NSData *putNSData=[putData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSLog(@"NSData : %@",putNSData);

NSString *putLength =[NSString stringWithFormat:@"%lu",(unsigned long)[putData length]];
NSLog(@"%@",putLength);

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"PUT"];
[request setValue:putLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:putNSData];

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

[connection start];

if (connection)
{
    putNSData=[[NSMutableData alloc]init];

}

  }

This is my delegate method :

       -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *Success=[dict valueForKey:@"Success"];
NSString *AccessToken=[dict valueForKey:@"AccessToken"];
NSString *UserId=[dict valueForKey:@"UserId"];

NSLog(@"ReceiveData :Success : %@ \n AccessToken : %@ \n UserId : %@",Success,AccessToken,UserId);
    }

please tell me whats wrong in my code ......

Was it helpful?

Solution

This code works perfect. Do this for PUT:

- (void)PutMethod:(NSString *)url andPostData:(NSString *)putData {
    NSLog(@"%@", url);
    NSLog(@"%@", putData);

    NSData *putNSData = [putData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSLog(@"NSData : %@",putNSData);

    NSString *putLength = [NSString stringWithFormat:@"%lu", (unsigned long)[putData length]];
    NSLog(@"%@", putLength);

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"PUT"];
    [request setValue:putLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:putNSData];

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

    [connection start];

    if (connection) {
        putNSData = [[NSMutableData alloc] init];
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top