How to make a PUT HTTP request in ios sdk? - ios

In my application I need to make a PUT type request. I have followed the below steps
NSString *path = #"http://hostname/api/Account/VerifyUser?applicationToken=72B648C6-B2B7-45ED-BA23-2E8AAA187D2C&emailID=xxx#gmail.com&password=aaaaa";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:path]];
[request setHTTPMethod:#"PUT"];
[request setValue:[NSString stringWithFormat:#"%d", string.length] forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded;charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:[string dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
For POST and GET I am getting correct output.
I have searched a lot but i am not getting any solution.Please suggest me any possible solution .
Thanks in advance

Try this
For xml request body
Set [request setValue:#"application/x-www-form-urlencoded;charset=utf-8;application/form-data" forHTTPHeaderField:#"Content-Type"];
OR
For json request body
Set [request setValue:#"application/x-www-form-urlencoded;charset=utf-8;application/json" forHTTPHeaderField:#"Content-Type"];

Related

NSMutableUrlRequest returning null value

I have a string with webservice.But when i add the string into NSMutableRequest, The method return become null.Here is my code,
-(NSMutableURLRequest *)createRequest:(NSString *)convertedString{
NSString *temp=[NSString stringWithFormat:#"%#%#",Api_Link,convertedString];
NSLog(#"%#",temp);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:temp]];
[request setHTTPBody:[convertedString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"App IPHONE" forHTTPHeaderField:#"USER_AGENT"];
[request setHTTPMethod:#"POST"];
return(request);
}
I ran your code in an Xcode Project:
NSString *temp = #"https://www.google.se/";
NSString *temsssp = #"https://www.google.se/";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:temp]];
[request setHTTPBody:[temsssp dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"App IPHONE" forHTTPHeaderField:#"USER_AGENT"];
[request setHTTPMethod:#"POST"];
NSLog(#"%#", request);
Result:
2017-02-15 06:43:23.061816 Sneak[5023:3002603] <NSMutableURLRequest: 0x170218120> { URL: https://www.google.se/ }
So there is nothing wrong with your request if you have a valid URL.
If you need further details and help, provide further information and code on how you run this method, and I will update the answer accordingly as your question changes or is edited.
EDIT:
As I mentioned, you need a Valid URL, I did the NSLog for you with this line:
NSString *temp = #"somethingsomething";
And the results are nil. So again, you need a valid URL.

How to upload image to redmine server?

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

Posting to a Google Forms form using NSURLRequest

I am trying to post a string to a Google Form using NSURLRequest. The method seems to be getting called, but the post to the Google Form doesn't appear. This is the code I am using:
NSURL *requestURL = [NSURL URLWithString:#"https://docs.google.com/forms/d/1T9M6B_k4tiQcSPP5iS2jyU7DCkoRC1jFVql_eQDy9ek/formResponse"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[request setHTTPMethod:#"POST"];
NSString *postString = #"entry.757751040=Test sent from iOS App";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
if (connection){
NSLog(#"Connecting...");
}
I created this form solely for testing purposes. "Connecting..." gets logged, so I know the connection is being made, but the string does not appear in the Google Forms Spreadsheet. Am I doing something wrong? Any help would be greatly appreciated.
This piece of code only checks if connection is not nil
if (connection){
NSLog(#"Connecting...");
}
is equivalent to
if (connection != nil){
NSLog(#"Connecting...");
}
As such, it has nothing to do with an actual connection to the URL.
The method initWithRequest:(NSURLRequest *)request delegate:(id)delegate which you are using will perform the request immediately. This means that you have to perform the operations in another order if you want your params to work:
NSURL *requestURL = [NSURL URLWithString:#"https://docs.google.com/forms/d/1T9M6B_k4tiQcSPP5iS2jyU7DCkoRC1jFVql_eQDy9ek/formResponse"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
NSString *postString = #"entry.757751040=Test sent from iOS App";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Along with this, consider implementing NSURLConnectionDelegate.

Fields to be set in HTTP header

I am calling a web service in iOS. For this, I need to set the header in NSMutableURLRequest object. My service takes two string parameters and returns data in JSON format.
What are the the fields that I need to set in the header (Both while using GET and POST) using the setValue:forHTTPHeaderField:.
We do not need to use setHTTPBody: while using GET.. right???
I had the same question and I resolved (using some code from Iducool and Ankit Mehta) like this...
NSURL *theURL = [NSURL URLWithString:#"yourURL"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];
//Specify method of request(Get or Post)
[theRequest setHTTPMethod:#"GET"];
//Pass some default parameter(like content-type etc.)
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"application/json; charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
//Now pass your own parameter
[theRequest setValue:yourValue forHTTPHeaderField:theNameOfThePropertyValue];
NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
//Now you can create a NSDictionary with NSJSONSerialization
NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:theResponseData options:0 error:&theError];
NSLog(#"url to send request= %#",theURL);
NSLog(#"%#",dataDictionaryResponse);
Look at this code which i am using to call a web service and thats working for me
+(NSString *)http_post_method_changed:(NSString *)url content:(NSString *)jsonContent{
NSURL *theURL = [NSURL URLWithString:url];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];
NSData *requestData = [jsonContent dataUsingEncoding:NSUTF8StringEncoding];
[theRequest setHTTPMethod:#"POST"];
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"application/json; charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
[theRequest setValue:[NSString stringWithFormat:#"%d", [requestData length]] forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPBody: requestData];
NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
NSString *data=[[NSString alloc]initWithData:theResponseData encoding:NSUTF8StringEncoding];
NSLog(#"url to send request= %#",url);
NSLog(#"response1111:-%#",data);
return data;
}
Both while using GET and POST We do not need to use setHTTPBody?
setHTTPBody only make sense when you are using POST request. GET don't need it.
For Header parameters do something like below
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];
//Specify method of request(Get or Post)
[theRequest setHTTPMethod:#"GET"];
//Pass some default parameter(like content-type etc.)
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"application/json; charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
//Now pass your own parameter
[theRequest setValue:yourObj forHTTPHeaderField:#"your parameter name"];
setHTTPBody is used when HTTP method POST is being used.
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:data];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Here the data is nothing but NSData of JSON data that need's to be sent.

IOS: ASIFormDataRequest with quotation marks

I have to send json to a web service that only accepts it via a POST variable.
ASIFormDataRequest insists on escaping my quotation marks.
any help would be appreciated
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSString *body = [NSString stringWithFormat:#"{\"user\":\"username\",\"pass\":\"password\"}"];
[request setPostValue:body forKey:#"body"];
[request startSynchronous];
output: "{\"user\":\"username\",\"pass\":\"password\"}"
Try Converting the parameter into JSON representation before sending it through SBJSON or whatever JSON parser you are using.
JosephH is right
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[dataString dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];
[request startSynchronous];
was the way to go.

Resources