2

I'm having an issue when i'm trying to send JSON request with an iOS app. I have to send something like this :

http://url/User/Create/{"newsletter" : 1,"password" : "sdsd", ...}

But the request isn't valid when doing this on the app :

 NSURL   *url = [NSURL URLWithString:@"http://url/User/Create/"];
NSData *jsonData;

NSDictionary *dic = @{
                      @"login" : @"sdsd",
                      @"password" : @"sdsd",
                      @"newsletter" : @1,
                      @"firstname" : @"sdsd",
                      @"lastname" : @"sdsd",
                      @"email" :  @"sdsd@hotmail.fr"};
jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&error];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: jsonData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];

NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];

How can i send the request in a valid format ?

iPatel
  • 46,010
  • 16
  • 115
  • 137
Gezurb
  • 143
  • 2
  • 12
  • Can you post the specific error? – robbie_c Feb 26 '14 at 10:34
  • It doesn't throw any error, but when i try to log in with the login and password, i get a {"result":"ko"}, but it works when i try to register through the browser – Gezurb Feb 26 '14 at 10:37
  • The POST request seems to be correct. Nonetheless, there might occur a number if errors when you send the request: the url could be malformed, the server is down, the server accepts a JSON but does not semantically understand the content, authentication failed, etc. You really should check errors whenever possible! And especially if the server returned a response:check status code and MIME type! – CouchDeveloper Feb 26 '14 at 11:27
  • You use `POST` but in your URL example it looks more like `GET`. What do you need exactly? – toasted_flakes Feb 26 '14 at 12:07
  • Note that you are using an insecure connection, and thus your password an other confidential data will be sent in clear. Using a secure connection via https, will not work that way using `sendSynchronousRequest` - since this method requires the credentials to be specified in the URL. – CouchDeveloper Feb 26 '14 at 12:19
  • Note that when you send data as part of a URL it must be appropriately "escaped". – Hot Licks Feb 26 '14 at 12:52

2 Answers2

1
-(void)tryThis  
{  
NSString *a=@"{'login' : 'sdsd', 'password' : 'sdsd', 'newsletter' : 1, 'firstname' : 'sdsd', 'lastname' : 'sdsd',  'email' :  'sdsd@hotmail.fr'}";    
a= [a stringByReplacingOccurrencesOfString:@"'" withString:@"\"" options:NSCaseInsensitiveSearch range:NSMakeRange (0, [a length])];  
NSData* postData= [a dataUsingEncoding:NSUTF8StringEncoding];  
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https:***********your url"]];  
[request setHTTPMethod:@"POST"];  
[request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"];  
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];   
[request setHTTPBody:postData];  
NSURLResponse *response = NULL;   
NSError *requestError = NULL;   
NSData *responseData1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];   
NSString *responseString = [[NSString alloc] initWithData:responseData1 encoding:NSUTF8StringEncoding];   
 }  
Charan Giri
  • 1,097
  • 1
  • 9
  • 15
  • Why do you suggest to use a wonky approach to construct a JSON - and not use NSJSONSerialization to do the job? – CouchDeveloper Feb 26 '14 at 11:31
  • i got the same issue what he got now and this fixed my issue, I thought it will be helpful. – Charan Giri Feb 26 '14 at 11:38
  • So, you are saying `NSJSONSerialization` is buggy? – CouchDeveloper Feb 26 '14 at 11:55
  • no, some times our coding format depends on server side code(how they code) NSJSONSerialization is good i used it in almost 38 applications but the above code is the only one which worked for me when NSJSONSerialization did work(as i said depends on server side request we need to code) – Charan Giri Feb 26 '14 at 12:00
  • But you did specify `application/json` as content type. And the simplest way to get a sequence of bytes (a NSData whose content is in UTF-8) from a JSON compatible NSDictionary or NSArray is using NSJSONSerialization. So, I don't understand your approach, even though you assert that NSJSONSerialization is correct. Note: Possibly adding a charset parameter, e.g.: `application/json; charset=utf-8` may fix *your* issue, if the server is unable to deduce the encoding from the given JSON - but in general it _should_. – CouchDeveloper Feb 26 '14 at 12:09
1

HttpRequestSenderDelegate.h

@protocol HttpRequestSenderDelegate <NSObject>

-(void) didFinishLoading:(BOOL)success data:(NSData*)data;

@end

HttpPostRequestSender.h

@protocol HttpRequestSenderDelegate;
@interface HttpPostRequestSender : NSObject

- (id)initWithDelegate:(id<HttpRequestSenderDelegate>) delegate;
- (void)send:(NSString*)url postParams:(NSString*)postParams;
- (void)cancelDownload;

@end

HttpPostRequestSender.m

#import "HttpPostRequestSender.h"
        #import "HttpRequestSenderDelegate.h"

        @interface HttpPostRequestSender ()
        @property (nonatomic, weak) id<HttpRequestSenderDelegate> delegate;
        @property (nonatomic, strong) NSMutableData *activeDownload;
        @property (nonatomic, strong) NSURLConnection *dataConnection;
        @end

        @implementation HttpPostRequestSender
        @synthesize delegate = _delegate,
                    activeDownload = _activeDownload,
                    dataConnection = _dataConnection;

        - (id)initWithDelegate:(id<HttpRequestSenderDelegate>) delegate
        {
            self = [super init];
            if (self) {
                self.delegate = delegate;
            }
            return self;
        }

        - (void)cancelDownload
        {
            [self.dataConnection cancel];
            self.dataConnection = nil;
            self.activeDownload = nil;
        }

        - (void)send:(NSString*)url postParams:(NSString*)postParams
        {
            NSURL *nsurl = [NSURL URLWithString:url];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl
                                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                               timeoutInterval:60.0];
            [request setHTTPMethod:@"POST"];
            [request setHTTPBody:[postParams dataUsingEncoding:NSUTF8StringEncoding]];
            NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
            self.dataConnection = conn;

        }

        - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
        {
            self.activeDownload = [[NSMutableData alloc] init];
        }

        - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
        {
            [self.activeDownload appendData:data];
        }

        - (void)connectionDidFinishLoading:(NSURLConnection *)connection
        {
            [self.delegate didFinishLoading:YES data:self.activeDownload];
            self.dataConnection = nil;
            self.activeDownload = nil;
        }

        - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
        {
            UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to either 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
            [errorView show];
            [self.delegate didFinishLoading:NO data:nil];
            self.dataConnection = nil;
            self.activeDownload = nil;
        }

        @end

in your view controller : MyViewController.h

#import <UIKit/UIKit.h>
#import "HttpRequestSenderDelegate.h"

    @interface MyViewController : UIViewController <HttpRequestSenderDelegate>
    @end

MyViewController.m

    #import "MyViewController.h"
        #import "HttpPostRequestSender.h"


        @interface MyViewController ()
        @property (nonatomic, strong) HttpPostRequestSender *requestSender;
        @end

        @implementation MyViewController
        @synthesize requestSender = _requestSender;

    -(HttpPostRequestSender*) requestSender
    {
        if(_requestSender == nil) _requestSender = [[HttpPostRequestSender alloc] initWithDelegate:self];
        return _requestSender;
    }

- (void)viewDidLoad
{
     //for example i send the request in the view did load

     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
     NSString *url = @"your url";
     //post params
     NSString *postParams =@"login=sdsd&password=sdsd&email=sdsd@hotmail.fr";
     [self.requestSender send:url postParams:postParams];
}           

- (void) didFinishLoading:(BOOL)success data:(NSData *)data
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    if(success && data != nil) {
          //here you can parse the response
    }
}

@end
poyo fever.
  • 742
  • 1
  • 5
  • 22