NSString * aToken = [[[NSUserDefaults standardUserDefaults] arrayForKey:#"accessToken"] firstObject];
NSString * post = [NSString stringWithFormat:#"code=%#", #"035571"];
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString * postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"url"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
[request setValue:#"no-cache" forHTTPHeaderField:#"Cache-Control"];
[request setValue:[NSString stringWithFormat:#"Bearer %#", aToken] forHTTPHeaderField:#"Authorization"];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[conn start];
the error is: {"error":"invalid_request","error_description":"Required field is missing. 'code'"}
how to fix it? any idea?
Related
I am using https://gist.github.com/rtt/10403467 for reference but I am not able to login into Tinder. I get a blank response.
NSURL *url = [NSURL URLWithString:#"https://api.gotinder.com/auth"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
//NSString *str = [NSString stringWithFormat:#"{\"facebook_token\": %#, \"facebook_id\": %#}",accessToken,userID];
NSDictionary *dataDict = [NSDictionary dictionaryWithObjectsAndKeys:accessToken, #"facebook_token", userID, #"facebook_id", nil];
NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:dataDict
options:NSJSONReadingAllowFragments
error:nil];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
//[request setHTTPBody:[str dataUsingEncoding:NSASCIIStringEncoding]];
[request setHTTPBody:dataFromDict];
[request setValue:#"Tinder/4.1.4 (iPhone; iOS 9.3; Scale/2.00)" forHTTPHeaderField:#"User-Agent"];
[request setValue:#"ios" forHTTPHeaderField:#"platform"];
[request setValue:#"1.0" forHTTPHeaderField:#"app-version"];
[request setValue:[NSString stringWithFormat:#"%lu",[dataFromDict length]] forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
Please help
I have to make json post request with the following request parameters,
{
"method":"login",
"data":{
"username":"korea",
"password":"123456"
}
}
I use the following code to make the request,
NSString *jsonRequest = [NSString stringWithFormat:#"{""\"\method:\"\login\"\"\,""\data:\"{\"\"username\":\"%#\",\"password\":\"%#\"}",username,password];
NSLog(#"Request: %#", jsonRequest);
NSURL *url = [NSURL URLWithString:#"http://myurl..."];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [requestData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody: requestData];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
I have to get the response but Im not be able to get the response
Try this
NSDictionary *objectDic = #{#"username" : username, #"password" : password};
NSDictionary *dataDic = #{#"data" : objectDic};
NSDictionary *methodDic = #{#"method" : #"login", #"data": dataDic};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:methodDic
options:NSJSONWritingPrettyPrinted
error:nil];
[request setHTTPBody: requestData];
I tried sending the details(referring this link http://www.redmine.org/projects/redmine/wiki/Rest_api) using the below code(the url is mylocalst:portnumber/uploads.json).But I am getting the 500 error message.What is the meaning of request body as file content While uploading the image to server?Can anyone please provide me some information regarding this?
` NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[dataObj length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
request.timeoutInterval = 60;
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/octet-stream" forHTTPHeaderField:#"CONTENT_TYPE"];
[request setValue:#"my api key" forHTTPHeaderField:#"X-Redmine-API-Key"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:dataObj];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
receivedData = [[NSMutableData alloc] init] ;
}
Thanks in advance
i need to post my value to the test WebView i try'd but my value didnt post and no change while iam running
NSString *post = [NSString stringWithFormat:#"643c30f73a"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"XXXXX.com"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
Are you implementing the required delegate methods for NSURLConnection?
See the documentation at https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html for details and example implementations.
Implementing the delegate methods will at least give you an indication of any error that is occurring.
its worked for me,
NSURL *url = [NSURL URLWithString: #"http://your_url.com"];
NSString *body = [NSString stringWithFormat: #"arg1=%#&arg2=%#", #"val1",#"val2"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
I tried with below code. Here the problem is parameters are directly passing to the database from url.But i need to send the data from Text fields.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:#"http:UserProfile.svc/InsertUserDetailsNew?Name=RajeevLeader&FirstName=rajeev&DateofBirth=2012-02-03&Sex=M&Country=India&City=Banglore&PhoneNumber=8523898947&IncumbentonPremimum=true"]];
NSLog(#"url is %#",request);
NSString *requestString =[NSString stringWithFormat:#"Name=%#&FirstName=%#&DateofBirth=%#&Sex=%#&Country=%#&City=%#&PhoneNumber%#&IncumbentonPremimum=%#",name.text, firstname.text,dob.text,gender.text,country.text,city.text,phone.text,premiumtextfield.text];
NSLog(#" RequestString: %#",requestString);
NSMutableData *requestData =[NSMutableData dataWithBytes:[requestString UTF8String] length: [requestString length]];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody: requestData];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (con)
{
NSLog(#"data sent ");
} else
{
NSLog(#"Not sent");
}
[con start];
just change your URL for the following method
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:#"http://192.168.2.4:98/UserProfile.svc/InsertUserDetailsNew"]];
NSString *requestString = [[NSString alloc]
initWithFormat:#"{ \"Name\" : \"%#\" , \"FirstName\" : \"%#\" , \"DateofBirth\" : \"%#\" , \"Sex\" : \"%#\", \"Country\" : \"%#\", \"City\" : \"%#\", \"PhoneNumber\" : \"%#\" , \"FirstName\" : \"%#\"}",
name.text, firstname.text,dob.text,gender.text,country.text,city.text,phone.text,premiumtextfield.text];
NSLog(#" RequestString: %#",requestString);
NSMutableData *requestData =[NSMutableData dataWithBytes:[requestString UTF8String] length: [requestString length]];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody: requestData];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[con start];
you have to pass parameters afterwords & not with the url.