I am integrating Twilo sdk to my iPhone app.
NSString *urlString=#"urlstring";
NSURL* url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request startSynchronous];
NSString *response=[request responseString];
NSLog(#"Response:%#",response);
NSDictionary *dict=[response JSONValue];
NSMutableString *capabilityToken=[[NSMutableString alloc] initWithFormat:#"%#",[dict objectForKey:#"message"]];
device = [[TCDevice alloc] initWithCapabilityToken:capabilityToken delegate:nil];
I am getting response as
{"message":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJBQzYwYTFlOTMxOTI5NmFhNmFlZDcwZjhkYjZhMTQyNGJmIiwiZXhwIjoxMzM3NzU5MTA3LCJzY29wZSI6InNjb3BlOmNsaWVudDpvdXRnb2luZz9hcHBTaWQ9QVAxODRlNTE3YzZmN2EyZGI5NTkwMzM5N2I3NWRkMDliMSJ9.fXjQhBaXu3OlN_zXZIvSkoElphtQuW1QnNSbmUzfsSc"}
Assigning that string to TCDevice object while initializing I am getting following exception.
-[__NSCFString PJSTRString]: unrecognized selector sent to instance 0x77a21d0
Can anyone please help me the reason for this exception and how to solve it.
Thanks in advance.
Try adding the flags '-ObjC -all_load' to the "Other Linker Flags" in your target's Build Settings
Related
So, I've ran over and over the web in search for anything about sending XML with POST from iPhone app - no luck so far, none!
I'm using in my app KissXML, which I find very easy and useful when it comes to getting XML out of response - but quite opposite when sending XML to server...
Here is my method for connecting and receiving XML. I tried to put NSString containing simply my XML request into body of POST request, but it doesn't work as planned.
-(void)prepareTransaction{
NSLog(#"FXSecondVC: preparing transaction...");
NSString *login = [[NSUserDefaults standardUserDefaults] stringForKey:#"kUsername"];
NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:#"kPassword"];
NSString *host = [[NSUserDefaults standardUserDefaults] stringForKey:#"kURLServer"];
NSURL *url = [[NSURL alloc] initWithString:host];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setAuthorizationHeaderWithUsername:login password:password];
[httpClient registerHTTPOperationClass:[AFKissXMLRequestOperation class]];
NSString *xmlString = #"<RootEl xmlns=\"http://some.url/goes/here\">"
"<Element1>12678967.543233</Element1>"
"<Element2>"
"<string xmlns=\"bla.bla/url\">"
"String content</string>"
"<string xmlns=\"bla.bla/url\">"
"String content</string>"
"</Element2>"
"<Element3>true</Element3>"
"<Element4>String content</Element4>"
"<Element5>1999-05-31T11:20:00</Element5>"
"<Element6>true</Element6>"
"</RootEl>";
NSMutableURLRequest *request = [httpClient requestWithMethod:#"POST" path:kServerRequestURL parameters:nil];
[request setHTTPBody:[xmlString dataUsingEncoding:NSUTF8StringEncoding]];
AFKissXMLRequestOperation *operation = [AFKissXMLRequestOperation XMLDocumentRequestOperationWithRequest:request success:^(NSURLRequest *req, NSHTTPURLResponse *resp, DDXMLDocument *XMLDocument){
NSLog(#"[SUCCESS]: XMLDocument: %#", XMLDocument);
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, DDXMLDocument *XMLDocument) {
NSLog(#"error parsing: %#", [error localizedDescription]);
}];
[operation start];
}
This is what I'm getting in response:
2012-11-21 19:40:09.884 FXApp[19662:707] FXSecondVC: preparing transaction...
2012-11-21 19:40:10.011 FXApp[19662:707] error parsing: Expected status code in (200-299), got 400
Am I missing something here? I want to use KissXML, because it the simplest way (at least known to me) to use already prepared XML document in successful response, but if solution requires changing framework - don't hesitate. The priority is to get it working.
I hit dead end - this is driving me crazy, especially it is really urgent matter.
Mystery solved:
it appears that all I had to do was to set Content-Type for xml - which I wasn't doing. Solution found here
Here you go:
[request setValue:#"application/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
In my case, I had to use setValue:#"text/xml" to get the desired JSON response from the server (PHP server).
i.e. I used the following:
[request addValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
I'm getting this error trying to post a request to hattrick.org using SAOAuthTwitterEngine Oauth authentication.
With all the GET requests everything works fine, but only one call must be in POST with a JSON string and this one doesn't work.
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn't be completed. (NSURLErrorDomain error -1012.).
I'm sure the request must be in http!
Here the code.
NSURL *finalURL = [NSURL URLWithString:path];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:finalURL
consumer:self.consumer
token:_accessToken
realm:nil
signatureProvider:nil];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[[NSData alloc]initWithContentsOfFile:body]];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest: request delegate: self didFinishSelector:#selector(testApiCallResult:didFinish:) didFailSelector: #selector(testApiCallResult:didFail:)];
return responseBodyToReturn;
I don't know how to solve this error.
I think that you occurred in a bug on the library...
Try with another, my suggestion is to use this:
gdata-objectivec-client
I'm trying to upload a file using ASIFormDataRequest
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString: uploadUrl]] autorelease];
[request setPostValue:md5sum forKey:#"md5sum"];
[request setFile:[NSString stringWithFormat:#"%#", filePath] forKey:#"database"];
[request startSynchronous];
This logs an error:
"No file exists at: file://localhost/Users/thomas/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/36115C00-9352-4CD4-B5C2-C3A1CD6041FF/Documents/foo.sqlite"
However when I try the path in my browser. It downloads the file straight away, so the file does exist.
Anyone got a clue?
Thanks!
Try to use POSIX path:
[request setFile:[[NSURL URLWithString:filePath] path] forKey:#"database"];
I suspect it's the %20 that makes troubles. Try unescaping the filePath variable:
filePath = [filePath stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
I'm doing an app which retrieves user info, and it does that using a GET request, but I don't know how could I make one. Already tried ASIHTTPRequest with this code:
NSURL *url = [NSURL URLWithString:#"http://forrst.com/api/v2/users/info"];
// Now, set up the post data:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:#"1" forKey:#"id"];
[request setRequestMethod:#"GET"];
// Initiate the WebService request
[request setDelegate:self];
[request startAsynchronous];
But I get this response from forrst:
2011-06-05 16:59:32.189 Forrsty[4335:207] {"resp":{"error":"you must pass either id or username"},"stat":"fail","in":0.0557,"authed":false,"authed_as":false,"env":"prod"}
Which I understand I'm not doing the GET request ok, so how I would do it? Thanks :)
A Get Parameter ?
So why don't you try "http://forrst.com/api/v2/users/info?id=1" ?
[ NSString stringWithFormat:#"http://forrst.com/api/v2/users/info?id=%d", 1 ];
By the way, take a look at this librayry : http://allseeing-i.com/ASIHTTPRequest/
Good luck !
I'm adding a Basic HTTP Authorisation Header into a request but need to encode the authString to Base64. For info, I can't use didReceiveAuthenticationChallenge due to excessive 401 errors being produced.
The code below works fine in iOS 4.2 but doesn't work on iOS 3.2 (and I want to support this).
NSString *authString = [[[NSString stringWithFormat:#"%#:%#", user, password] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];
authString = [NSString stringWithFormat: #"Basic %#", authString];
NSMutableURLRequest* request =
[[NSMutableURLRequest alloc] initWithURL: url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 30];
[request setValue: authString forHTTPHeaderField: #"Authorization"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
In the first line of my code above I get the warning that NSData will not respond to 'base64Encoding'.
So I've downloaded the custom class NSData+Base64 from here:
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
But......I don't know how to use this class to convert my NSString (authString). Please help?!
I think the following line of code should fix:
NSString *authString = [[[NSString stringWithFormat:#"%#:%#", user, password] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
but I get following message:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData base64EncodedString]: unrecognized selector sent to instance
Have I missed an Import or something?
P.S. This is my first question on here, so go easy on me!!
NSString has a -dataUsingEncoding: method which you can use to convert NSString instances to NSData instances. After that, you can use MG's Base64 category.
Have you created the category to use the BASE64Encoding method in your NSData class?? here i made a tutorial to easy create categories, which basically are modifications of an existing class, in this case NSData: http://www.donttouchmycode.com/objective-c-class/extending-an-existing-class-with-categories i hope this can help you.