I must be missing something basic because I am unable to get any NSURLSession examples using POST to work at all. I have my server set up to print out (to a file that I tail) all the received POST parameters and nothing I put in the POST body shows up. I've tried the solutions from Send POST request using NSURLSession as well as online tutorials such as the Ray Wenderlich Cookbook for using NSURLSession.
Here, for example, is the code almost directly from the Stackoverflow thread, mentioned above, with only the URL and the post arguments changed:
-(void)postTest {
NSString *textContent = #"XXXXX";
NSString *noteDataString = [NSString stringWithFormat:#"x=%#", textContent];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = #{
#"a" : #"YYYYY"
};
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURL *url = [NSURL URLWithString:#"[MY URL with PHP script]"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPBody = [noteDataString dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPMethod = #"POST";
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
outputLabel.text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}];
[postDataTask resume];
}
The PHP script shows the "XXXXX" parameter was properly received - but it's not part of the POST body; rather, it is part of the URL itself. The only parameter in the POST body is the "YYYYY" parameter but it doesn't show up at all.
The Ray Wenderlich example didn't work either: nothing showed up for the PHP script.
-(void)testPost {
NSURL *url = [NSURL URLWithString:#"[MY URL with PHP script]"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = #"POST";
NSDictionary *dictionary = #{#"a": #"YYYYY"};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
if (!error) {
NSURLSessionUploadTask *uploadTask =
[session uploadTaskWithRequest:request
fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
}];
[uploadTask resume];
}
}
Is there something I'm not setting somewhere? I hadn't expected the shift to NSURLSession would have such subtle boobytraps and I'm wondering if it's something silly I'm doing wrong or missing. Thanks for any help!
Apple Documentation about the request parameter on uploadTaskWithRequest:fromData:completionHandler:
An NSURLRequest object that provides the URL, cache policy, request
type, and so on. The body stream and body data in this request object
are ignored.
Related
I want to make a PUT request to a URL, but when the output shows status code as 405, which means the request to the URL is something other than put.
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:#"http://httpbin.org/put"];
NSMutableURLRequest *request =[[NSMutableURLRequest alloc]initWithURL:url];
NSData *postbody = [#"name=testname&suggestion=testing123" dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
request.HTTPMethod = #"PUT";
[request setHTTPBody:postbody];
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil)
{
[request setHTTPBody:data];
NSLog(#"Response = %#",response);
}
}];
[dataTask resume];
Can someone point out where i am going wrong, i have been reading a lot about this issue since the last couple of hours, but i am not able to figure it out. Kindly do not mark this as duplicate since the previous op did not add body which is not the case with my code. Also the URL mentioned accepts any data as body, so i guess what i set the data to is irrelevant.
EDIT (ANSWER):
After banging my head from yesterday, one of my senior helped me solve the issue, hope this will help someone. The data task needs to be supplied with the request object and not with the URL, this was the reason it always showed 'GET' in Charles web debugging tool. The code should be as follows:
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// code
}];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = #"PUT";
When I try to call web service using NSURLSession with POST on Apple Watch, response parameter is nil. What might be the issue with this ?
Code for calling web service:
NSURL *url = [NSURL URLWithString:#"https://demo.test.com:22322/api/Alerts/UpdateAlertStatus?intUserId=5¶maccessTokenId=24460c5f-be71-45b5-99cf-f46c277c3d9e&UserId=100200"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = #"POST";
NSMutableDictionary *mutDictParameters = [[NSMutableDictionary alloc] init];
[mutDictParameters setObject:#"708" forKey:#"AlertId"];
[mutDictParameters setObject:#"2878" forKey:#"XmlMessageId"];
[mutDictParameters setObject:#"5" forKey:#"UserId"];
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:mutDictParameters
options:kNilOptions error:&error];
if (!error) {
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
// Handle response here
NSLog(#"Response : %#",response);
}];
[uploadTask resume];
}
I have a client ID and secret that I am supposed to send in a post request in order to receive a token to access an API (OAuth). I am using:
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
to setup my authorization headers; however, I keep returning:
{
error = unauthorized;
"error_description" = "Full authentication is required to access this resource";
}
I have double and triple checked my client ID and secret, but I keep getting this response.
Here is the code that I am using to make my request:
NSURL *baseURL = [NSURL URLWithString:#"http://"];
NSURL *url = [NSURL URLWithString: #"/oauth/token?" relativeToURL:baseURL];
NSURL *absoluteURL = [url absoluteURL];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = #{#"Authorization" : [CLIENT_SECRET_PREFIX stringByAppendingString:[self base64Encode:CLIENT_SECRET_TOKEN]]};
sessionConfiguration.HTTPAdditionalHeaders = #{#"Content-Type" : #"application/json"};
NSURLSession *session =[NSURLSession sessionWithConfiguration:sessionConfiguration];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:absoluteURL];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"%#", json);
}] resume];
Can anyone please help me to solve this problem ? Thanks in Advance.
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];
}}
i'm trying to send a POST request to my server with the following code:
NSURLSessionConfiguration *defaultConfigObj = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObj];
//NSURL *url = [NSURL URLWithString:#"http://www.myserver.com/page.aspx"];
NSURL *url = [NSURL URLWithString:#"http://10.47.72.40/test/page.aspx"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.10f];
NSString *params = [NSString stringWithFormat:#"data=%#&UserMail=%#",
InfoDictionary[#"ID"],
arrDatosMail[0]
];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"Response: %# error:%#", response, error);
if(error == nil){
NSString *textRepsonse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"Data: %#", textRepsonse);
}
}];
So, when a try with the local IP, it works fine (the server receives the POST request), but when I change the remote IP (with de www.myserver.com/page.aspx) the page receives the request with GET method, i don't know why :(,
What i'm doing wrong? Thanks in advance.
There is no problem. Request will be sent as POST.
I would recommend to use proxy to debug what is send exactly. On Mac Charles proxy is good one.
Charles proxy web site