I want to send image and param to server, image should in base64 format.
Ex : my url : http://.............
Parameters: image = image (base64 format), filename = my image name
I tried with NSURLSessions, image sent to server but image bytes is ZERO.
my code is:
UIImage *dataImage = [UIImage imageWithData:strImageBase64];
NSString *ImageBase64 = [UIImagePNGRepresentation(dataImage) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:myurl];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params = [NSString stringWithFormat:#"image=%#&filename=%#",ImageBase64,#"unique2.png"];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"Response:%# %#\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(#"Data = %#",text);
}
}];
[dataTask resume];
Related
I am working in an application in which I have to send image to the server, I am trying to send Image to server but in return I am getting
BAD REQUEST 400
. Please tell me how do I resolve this error.
This method is use to convert image into base64 string
NSData * imagedata = UIImageJPEGRepresentation(chosenImage, 0.5);
NSString * base64String = [imagedata base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength
-(void)temp
{
NSString * str=[self base64return];
NSDictionary* jsonDict = #{
#"name": #"image_name",
#"img_data":str
};
NSData * postData = [NSJSONSerialization dataWithJSONObject:jsonDict
options:kNilOptions error:nil];
NSURL * url=[NSURL URLWithString:#"http://xxxxxx/finalresult1"];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask* task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if (error == nil)
{
// Success
NSLog(#"URL Session Task Succeeded: HTTP %ld", ((NSHTTPURLResponse*)response).statusCode);
NSString * text = [[NSString alloc] initWithData: data encoding:
NSUTF8StringEncoding];
NSLog(#"Data = %#",text);
NSLog(#"erroer is %#",error);
}
else
{
// Failure
NSLog(#"URL Session Task Failed: %#", [error localizedDescription]);
}
}];
[task resume];
}
Change request Content-Type, Use:
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
When i am using NSURLSession while Posting through the Browser is returning the result as 200 status but when i send it through code in IOS i am getting 500 status code as below.
Response:<NSHTTPURLResponse: 0x14e754240> { URL: urlAPI } { status code: 500, headers {
"Cache-Control" = private;
"Content-Length" = 30;
"Content-Type" = "text/plain; charset=utf-8";
Date = "Thu, 28 Jan 2016 12:59:10 GMT";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }
Below is my code
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:#"HERE IS MY URL"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params =#"MY PARAMETERS";
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"Response:%# %#\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(#"Data = %#",text);
}
}];
[dataTask resume];
This code worked previously but it is throwing error now(API code also not changed),where am i doing wrong.Help me out of this.
Try to send the parameters in NSDictionary:
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:#“[Your SERVER URL”];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request addValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setHTTPMethod:#"POST"];
NSDictionary *postData = [[NSDictionary alloc] initWithObjectsAndKeys: #“TestUservalue”, #"name",
#“TestPassvalue”, #“password”,
nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject: postData options:0 error:&error];
[request setHTTPBody: postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
[postDataTask resume];
The problem is with the parameters that you are sending to your webserver. They must be properly encoded. I was having the same 500 status code.
I was able to fix my problem by changing my post parameters. I removed the _ from my post variables name and it worked.
NSString * params = [NSString stringWithFormat:#"q_id=%#&c_id=%#&agent=%#",self.q_id, self.c_id, agent];
// changed to
NSString * params = [NSString stringWithFormat:#"qid=%#&cid=%#&agent=%#",self.q_id, self.c_id, agent];
Also check out this link on Objective-C encoding
I am trying to post a string to a server using the next example:
// 1
NSURL *url = [NSURL URLWithString:#"YOUR_WEBSERVICE_URL"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
// 2
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = #"POST";
// 3
NSDictionary *dictionary = #{#"key1": #"value1"};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary
options:kNilOptions error:&error];
if (!error) {
// 4
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
// Handle response here
}];
// 5
[uploadTask resume];
}
The difference is that I don't have a NSDictionary But an NSString object that stores an array of dictionaries, and also the string that I post must not be encoded it must be a simple string, so it is visible in the search field if I manually enter it.
My NSString example:
[{
"api_id" = debugger;
at = "2015-02-05T01:41:13Z";
oS = IOS;
ver = "8.10";
what = "showAdAt: forViewController:";
},
{
"api_id" = debugger;
at = "2015-02-05T01:41:13Z";
oS = IOS;
ver = "8.10";
what = "showAdAt: forViewController:";
}
]
Thank you in advance and be patient with me as this is my first post attempt.
I was thinking that the above example should work for me if I first convert the NSString to NSArray with dictionaries as objects.
UPDATE:
Currently I am trying to post the string as:
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = #{
#"Authorization" : #"CUSTOM AUTHORIZATION THAT I AM USING",
#"Content-Type" : #"application/json"
};
// Create the session
// We can use the delegate to track upload progress
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
// Data uploading task. We could use NSURLSessionUploadTask instead of NSURLSessionDataTask if we needed to support uploads in the background
NSURL *url = [NSURL URLWithString:#"MY WEBSITE LINK"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = #"POST";
//Convert the string to NSData
bodyContainerString = #"[{\"api_id\":\"A124\",\"at\":\"2011-04-10T20:09:31Z\",\"os\":\"ANDROID\",\"ver\":\"2.1\",\"what\":\"TEST\",\"value\":\"\"},{\"api_id\":\"A124\",\"at\":\"2011-04-10T20:10:31Z\",\"os\":\"ANDROID\",\"ver\":\"2.1\",\"what\":\"TEST\",\"value\":\"\"}]";
NSData* jsonData = [bodyContainerString dataUsingEncoding:NSUTF8StringEncoding];
jsonData = [jsonData subdataWithRange:NSMakeRange(0, [jsonData length] - 1)];
NSString* newStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(newStr);
request.HTTPBody = jsonData;
NSURLSessionDataTask *uploadTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// Process the response
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (!error && httpResp.statusCode == 201) {
//if no error on upload then delete content of plist
NSLog(#"Success on post1");
}
NSLog(#"Success on post2");
}];
[uploadTask resume];
Update 2, the resulting link should look like:
curl -X POST http://MY_LINK/smtg -H 'Authorization: CUSTOM_FIRST_HEADER' -H "Content-Type: application/json" -d '[{"api_id":"A124","at":"2011-04-10T20:09:31Z","os":"ANDROID","ver":"2.1","what":"TEST","value":""},{"api_id":"A124","at":"2011-04-10T20:10:31Z","os":"ANDROID","ver":"2.1","what":"TEST","value":""}]'
I ended up using the first example that I found here is my implementation:
NSURL *url = [NSURL URLWithString:#"MY_LINK/smtg"];
//Create thhe session with custom configuration
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = #{
#"Authorization" : [NSString stringWithFormat:#"BEARER %#",finalToken],
#"Content-Type" : #"application/json"
};
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
// 2
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = #"POST";
// 3
NSError *error = nil;
NSData* jsonData = [bodyContainerString dataUsingEncoding:NSUTF8StringEncoding];
if (!error) {
// 4
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:jsonData completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
// Handle response here
}];
// 5
[uploadTask resume];
}}
Since ASIHTTPRequest is deprecated, I am migrating my code to use NSURLSession based server communication. Currently, I am using NSDictionary "userInfo" property of ASIHTTPRequest to send additional user information. The description of "userInfo" in ASIHTTPRequest documentation is "Custom user information associated with the request (not sent to the server)".
After the request is processed, I re-fetch this "userInfo" object from the request object and take action accordingly.
My ASIHTTPRequest code example is
Request:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:#"http://www.google.com"];
[request setDelegate:self];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:#"requestCount"];
[request setUserInfo:userInfo];
I want to achieve same functionality through NSURLSession, how can I do this?
NSURLSession code example:
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: self.queue];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:timeOutSeconds];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"Response:%# %#\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(#"Data = %#",text);
}
}
}];
[dataTask resume];
Because you are using completion handler so you can use block variable, try below code:
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: self.queue];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:timeOutSeconds];
__block NSDictionary *userInfo = [NSDictionary dictionaryWithObjectAndKeys...........];**
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"Response:%# %#\n", response, error);
NSLog(#"UserInfo: %#", userInfo);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(#"Data = %#",text);
}
}
}];
[dataTask resume];
The cleanest way to go is to subclass NSURLRequest and add the desired property - be it tag, or userInfo and use it the same way you are used to with ASIHTTP framework.
I am a beginner in handling connections in iOS. I want to perform a POST request and a GET request to a url. The thing is that I need to first do the POST so I can get an access token parsed in a JSON that later will place in the header of the GET request, so I can retrieve the login data I need. I am using custom delegate methods, but when I run this, the GET request is performed earlier than the POST, so I cannot get the key before the GET is done. Is there any way to perform the POST first and then the GET? Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//params
self.user = #"carlos";
self.pass = #"1234";
self.grantType = #"password";
self.clientId = #"7da713e69afc96cf894e";
self.clientSecret = #"2c14ec54cfdfd6faec4ef56ca7f0870ab83f820b";
// Set the side bar button action. When it's tapped, it'll show up the sidebar.
self.sideBarButton.target = self.revealViewController;
self.sideBarButton.action = #selector(revealToggle:);
// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[self httpPostWithCustomDelegate];
[self sendHTTPGet];
}
-(void) httpPostWithCustomDelegate
{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:#"http://192.237.241.175:8090/oauth2/access_token/"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params =[NSString stringWithFormat:#"&username=%#&password=%#&grant_type=%#&client_id=%#&client_secret=%#&scope=write",self.user,self.pass,self.grantType,self.clientId,self.clientSecret];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"Retrieving Data");
// NSLog(#"Response:%# %#\n", response, error);
if(error == nil)
{
//Test Display
// NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
// NSLog(#"Data = %#",text);
self.responseDataPost = [[NSMutableData alloc] init];
[self.responseDataPost appendData:data];
NSError *error;
NSMutableDictionary *receivedData = [NSJSONSerialization JSONObjectWithData:self.responseDataPost options:NSJSONReadingMutableContainers error:&error];
//obtener del dictionary el access token y meter como header Authorization Bearer + id
//NSArray *accessToken = [receivedData valueForKeyPath:#"access_token"];
self.connTxtPost.text = [receivedData valueForKeyPath:#"access_token"];
self.recData = [receivedData valueForKeyPath:#"access_token"];
}
}];
[dataTask resume];
}
-(void) sendHTTPGet
{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:#"http://192.237.241.175:8090/snippets/"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:#"GET"];
self.connTxtGet.text = self.recData ;
[urlRequest setValue:self.recData forHTTPHeaderField:#"Authorization"];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//NSLog(#"Response:%# %#\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(#"Data = %#",text);
}
}];
[dataTask resume];
}
You don't need to call these two method all together.
[self httpPostWithCustomDelegate];
[self sendHTTPGet];
You are making two request at the same time. But which response will come first you can not determine it. As you need data from "POST" request to make the "GET" request. Do something like this:
-(void) httpPostWithCustomDelegate{
.............
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
...................
...................
self.connTxtPost.text = [receivedData valueForKeyPath:#"access_token"];
self.recData = [receivedData valueForKeyPath:#"access_token"];
[self sendHTTPGet];
}
}];
[dataTask resume];
}
This will make sure when you make the "GET" request you have the "access_token". Hope this will work just fine. :)