How to upload image to redmine server? - ios

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

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.

Login using Tinder API

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

SOAP web API request returns HTML instead of XML

I'm new to SOAP API.
I'm using the following code to access my API.
NSString *stringURL=[NSString stringWithFormat:#"http://tennispro.co.in/services.asmx"];
// Allocate and initialize an NSURLConnection with the given request and delegate. The asynchronous URL load process is started as part of this method.
NSString *soapMessage = [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<ReturnSyncMasterData xmlns=\"http://tempuri.org/\">"
"<datetime>%#</datetime>"
"</ReturnSyncMasterData>"
"</soap:Body>"
"</soap:Envelope>",#""];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stringURL]];
[request addValue:#"text/html; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:#"2159" forHTTPHeaderField:#"Content-Length"];
[request addValue:#"private, max-age=0" forHTTPHeaderField:#"Cache-Control"];
[request addValue:#"gzip" forHTTPHeaderField:#"Content-Encoding"];
[request addValue:#"http://tempuri.org/services/ReturnSyncMasterData" forHTTPHeaderField:#"SOAPAction"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:#"POST"];
theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if (theConnection) {
receivedData = [[NSMutableData alloc] init];
}
But in response I get HTML instead of XML. I want to access ReturnSyncMasterData soap action, but I'm unable to call it.
set content type to text/xml
[request addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:#"2159" forHTTPHeaderField:#"Content-Length"];
[request addValue:#"private, max-age=0" forHTTPHeaderField:#"Cache-Control"];
[request addValue:#"gzip" forHTTPHeaderField:#"Content-Encoding"];
[request addValue:#"http://tempuri.org/services/ReturnSyncMasterData" forHTTPHeaderField:#"SOAPAction"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:#"POST"];
theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if (theConnection) {
receivedData = [[NSMutableData alloc] init];
}

How to make a PUT HTTP request in ios sdk?

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"];

Miss parameter in http post

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?

Resources