I am trying to get the user agent but when I try and read it, it comes out (null)
NSLog(#"user agent = %#", [request valueForHTTPHeaderField: #"User-Agent"]);
request is an NSURLRequest. So I tried to get the http headers and I don't think there are any. When I use
NSLog(#"http headers = %d", [[req allHTTPHeaderFields] fileSize]);
it prints out zero. req is an NSMutableURLRequest. Does anyone know why this is happening.
This is the method that I am using:
- (BOOL)webView:(UIWebView )webView2 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSMutableURLRequest *req = (NSMutableURLRequest *)request;
NSString *versionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString)kCFBundleVersionKey];
NSLog(#"http headers = %#", [request allHTTPHeaderFields]);
NSLog(#"http headers = %d", [[req allHTTPHeaderFields] fileSize]);
[req setValue:[NSString stringWithFormat:#"myApp/%# %#", versionString, [request valueForHTTPHeaderField:#"User-Agent"]] forHTTPHeaderField:#"User-Agent"];
NSLog(#"user agent = %#", [request valueForHTTPHeaderField: #"User-Agent"]);}
This worked for me:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:#"navigator.userAgent"];
NSLog(#"navigator.userAgent = %#", secretAgent);
NSDictionary* headers = [request allHTTPHeaderFields];
NSLog(#"headers: %#",headers);
NSString* ua = [request valueForHTTPHeaderField:#"User-Agent"];
NSLog(#"User-Agent = %#", ua);
}
I don't know why you're looking at filesize when you can just look at the headers themselves.
Cf. https://stackoverflow.com/a/19184414/1431728
You don't have any headers in your request object because you haven't added any. If you want to specify the User-Agent header, you need to add it yourself, as outlined here.
Related
I send a registration message to the server like this:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10.0];
[request setHTTPMethod:#"POST"];
[request setValue:#"text/html" forHTTPHeaderField:#"Content-Type"];
[request setValue:API_KEY forHTTPHeaderField:#"api_key"];
NSString *dataString = [NSString stringWithFormat:#"username=%#&password=%#", username, encryptedPassword];
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
// Connection
NSURLResponse *response = nil;
NSError *error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// Print results
NSLog(#"response: %#", response);
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(#"response: %d", [(NSHTTPURLResponse *)response statusCode]);
}
id jsonObject = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:&jsonError];
NSLog(#"jsonObject: %#", jsonObject);
The connection was success, but I got the error response:
=> response: 400
=> jsonObject: {
error = "Please provide username.USERNAME:";
}
The server received a null user name, but I'm sure I pass a valid user name to http body (converted into NSData). The data contains the following info (converted back into string):
username=cc24#cc.com&password=12345678nW7wMNio0bsNBHGqY7VgSw%3D%3D
I don't what happened. But the API works fine with Android app.
I am trying to use an API From Mashape.
When I test the endpoint (JSON response), things are working well,
but the objective-c app-ready code example they give is not working, I get the following errors:
This is my code:
- (IBAction)loadJson:(id)sender
{
NSDictionary* headers = [NSDictionary dictionaryWithObjectsAndKeys:#"xxxxxxxxx", #"X-Mashape-Authorization", nil];
NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:[NSURL URLWithString:#"<file url>"], #"files", #"", #"urls", nil];
HttpJsonResponse* response = [[Unirest post:^(BodyRequest* request) {
[request setUrl:#"https://lambda-face-recognition.p.mashape.com/detect"];
[request setHeaders:headers];
[request setParameters:parameters];
}] asJson];
}
Is there any way to solve this?
Thanks!
The code examples show this is actually what the code should look like:
NSDictionary* headers = [NSDictionary dictionaryWithObjectsAndKeys:#"application/json", #"accept", nil];
NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:#"value", #"parameter", #"bar", #"foo", nil];
HttpJsonResponse* response = [[Unirest postEntity:^(BodyRequest* request) {
[request setUrl:#"http://httpbin.org/post"];
[request setHeaders:headers];
// Converting NSDictionary to JSON:
[request setBody:[NSJSONSerialization dataWithJSONObject:headers options:0 error:nil]];
}] asJson];
Or, maybe this one:
NSDictionary* headers = [NSDictionary dictionaryWithObjectsAndKeys:#"application/json", #"accept", nil];
NSURL file = nil;
NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:#"value", #"parameter", file, #"file", nil];
HttpJsonResponse* response = [[Unirest post:^(MultipartRequest* request) {
[request setUrl:#"http://httpbin.org/post"];
[request setHeaders:headers];
[request setParameters:parameters];
}] asJson];
I am trying to send a post value to my server.
It successfully send English characters.
This code is for finding friend, and it can't send UTF-8 text.
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
/*
Update the filtered array based on the search text and scope.
*/
if(![searchText isEqualToString:#""]){
NSUserDefaults * userdef = [NSUserDefaults standardUserDefaults];
NSString *encodedSearchText = [searchText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString * uploadUrl = [NSString stringWithFormat:#"%#/%#" , GPON_URL , GPON_URL_SEARCHFRIEND];
NSURL *url = [NSURL URLWithString:uploadUrl];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:encodedSearchText forKey:#"u_id"];
[request setPostValue:[userdef objectForKey:KEY_USER_ID] forKey:#"s_username"];
[request startSynchronous];
[request setShouldAttemptPersistentConnection:NO];
NSString *response = [request responseString];
SBJsonParser * parser = [[[SBJsonParser alloc] init] autorelease];
NSDictionary * json = (NSDictionary *)[parser objectWithString:response];
[searchMember removeAllObjects];
if( [[json objectForKey:#"result"] isEqual:#"YES"] ){
[searchMember addObjectsFromArray:[json objectForKey:#"data"]];
[self.tableView reloadData];
}
}
}
here is my code. can you see what cause the error?
You don't appear to be setting the string encoding of the request; try:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setStringEncoding:NSUnicodeStringEncoding];
Also, this is a very poor way of determining if an NSString is empty:
if (![searchText isEqualToString:#""])
Use the length method instead:
if ([searchText length] > 0)
I am using ASIHTTPRequest on the iPhone to make a call to the server, which then sends a response containing a JSON file. I got it working on a basic level, but when I added it to my project, and made a call to the server, my app started crashing with bad_access.
I make the call from viewDidLoad as such:
requestModel = [[RequestModel alloc]init];
NSURL *url = [[NSURL URLWithString:#"myURL"]retain];
[requestModel eventsRequestFor:#"myUsername" password:#"myPassword" forURL:url];
This calls a function in another class called RequestModel, and I go through and try to unpack the returned array as such (a lot of code):
-(void)eventsRequestFor:(NSString*)username password:(NSString*)password forURL:(NSURL*)forURL {
//use ASIHTTPRequest to post to sever. Here you authenticate and recieve answer
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:forURL];
[request setUsername:username];
[request setPassword:password];
[request setDelegate:self];
[request setDidFailSelector:#selector(uploadFailed:)];
[request setDidFinishSelector:#selector(uploadFinished:)];
[request startAsynchronous];
[request release];
NSLog(#"Model called");
}
#pragma mark Model Delegates
- (void)uploadFinished:(ASIHTTPRequest *)request {
// Use when fetching text data
NSLog(#"upload start");
NSString *stringFromRequest = [[NSString alloc]init];
stringFromRequest = [request responseString];
[self buildArrayFromRequest:stringFromRequest];
//[stringFromRequest release];
}
- (void)uploadFailed:(ASIHTTPRequest *)request {
NSString *statusMessage = [request responseStatusMessage];
NSLog(#"%#",statusMessage);
NSError *error = [request error];
NSLog(#"%#",error);
}
-(void)buildArrayFromRequest:(NSString*)string {
NSArray *arrayFromData = [[NSArray alloc]init];
arrayFromData = [string yajl_JSON];
NSLog(#"This is the array from the JSON %#",[arrayFromData objectAtIndex:0]);
NSMutableArray *events = [[NSMutableArray alloc] init];
for(int i= 0; i < [arrayFromData count];i++)
{
/////code to unpack array
}
///here i try to populate table with unpacked array
FirstViewController *firstVC = [[FirstViewController alloc]init];
[firstVC createTableWithArray:events];
[firstVC release];
[arrayFromData release];
[events release];
}
Any ideas on where I am going wrong? It seems like I am releasing an object that has already been released, but I can't find it.
EDIT: You're releasing an NSData that's not yours.
You don't need this line :
[arrayFromData release];
because of this mistake :
You have put :
NSArray *arrayFromData = [[NSArray alloc]init];
arrayFromData = [string yajl_JSON];
when all you need is this :
NSArray *arrayFromData = [string yajl_JSON]; // This doesn't need releasing
You're releasing a request that's already autoreleased :(
// Create an autoreleased request object
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:forURL];
...
[request release]; // fail here - this line is not needed
Just FYI :
This is unneeded code :
NSString *stringFromRequest = [[NSString alloc]init];
stringFromRequest = [request responseString];
You are making a string object and then getting a different one. The first one you're making will be leaked. This code will do what you want :
NSString *stringFromRequest = [request responseString]; // This doesn't need releasing :)
However, it's the cause of a leak, not the crash that you are reporting.
(You've got this pattern in other places in your code, not just here.)
This code snippet isn't working, I'm getting an "Authentication Failed." response from the server. Any ideas?
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:
[NSURL URLWithString:#"http://www.tumblr.com/api/write"]];
[request setHTTPMethod:#"POST"];
[request addValue:_tumblrLogin forHTTPHeaderField:#"email"];
[request addValue:_tumblrPassword forHTTPHeaderField:#"password"];
[request addValue:#"regular" forHTTPHeaderField:#"type"];
[request addValue:#"theTitle" forHTTPHeaderField:#"title"];
[request addValue:#"theBody" forHTTPHeaderField:#"body"];
NSLog(#"Tumblr Login:%#\nTumblr Password:%#", _tumblrLogin, _tumblrPassword);
[NSURLConnection connectionWithRequest:request delegate:self];
[request release];
Both _tumblrLogin and _tumblrPassword are run through stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding elsewhere in my code. My login email is of the form "address+test#test.com". It works just fine for logging in directly to tumblr, but I'm wondering if the "+" character is causing problems with the encoding? It's not being escaped. Should it be?
Thanks to Martin's suggestion, I'm now using CFURLCreateStringByAddingPercentEscapes to escape my login and password. I'm still having the same issue, though, my Authentication is failing.
The problem is that you are not creating a proper HTTP POST request. A POST request requires a properly formatted multipart MIME-encoded body containing all the parameters you want to send to the server. You are trying to set the parameters as HTTP headers which won't work at all.
This code will do what you want, note especially the NSString categories that create a valid Multipart MIME string:
#interface NSString (MIMEAdditions)
+ (NSString*)MIMEBoundary;
+ (NSString*)multipartMIMEStringWithDictionary:(NSDictionary*)dict;
#end
#implementation NSString (MIMEAdditions)
//this returns a unique boundary which is used in constructing the multipart MIME body of the POST request
+ (NSString*)MIMEBoundary
{
static NSString* MIMEBoundary = nil;
if(!MIMEBoundary)
MIMEBoundary = [[NSString alloc] initWithFormat:#"----_=_YourAppNameNoSpaces_%#_=_----",[[NSProcessInfo processInfo] globallyUniqueString]];
return MIMEBoundary;
}
//this create a correctly structured multipart MIME body for the POST request from a dictionary
+ (NSString*)multipartMIMEStringWithDictionary:(NSDictionary*)dict
{
NSMutableString* result = [NSMutableString string];
for (NSString* key in dict)
{
[result appendFormat:#"--%#\r\nContent-Disposition: form-data; name=\"%#\"\r\n\r\n%#\r\n",[NSString MIMEBoundary],key,[dict objectForKey:key]];
}
[result appendFormat:#"\r\n--%#--\r\n",[NSString MIMEBoundary]];
return result;
}
#end
#implementation YourObject
- (void)postToTumblr
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:
[NSURL URLWithString:#"http://www.tumblr.com/api/write"]];
[request setHTTPMethod:#"POST"];
//tell the server to expect 8-bit encoded content as we're sending UTF-8 data,
//and UTF-8 is an 8-bit encoding
[request addValue:#"8bit" forHTTPHeaderField:#"Content-Transfer-Encoding"];
//set the content-type header to multipart MIME
[request addValue: [NSString stringWithFormat:#"multipart/form-data; boundary=%#",[NSString MIMEBoundary]] forHTTPHeaderField: #"Content-Type"];
//create a dictionary for all the fields you want to send in the POST request
NSDictionary* postData = [NSDictionary dictionaryWithObjectsAndKeys:
_tumblrLogin, #"email",
_tumblrPassword, #"password",
#"regular", #"type",
#"theTitle", #"title",
#"theBody", #"body",
nil];
//set the body of the POST request to the multipart MIME encoded dictionary
[request setHTTPBody: [[NSString multipartMIMEStringWithDictionary: postData] dataUsingEncoding: NSUTF8StringEncoding]];
NSLog(#"Tumblr Login:%#\nTumblr Password:%#", _tumblrLogin, _tumblrPassword);
[NSURLConnection connectionWithRequest:request delegate:self];
[request release];
}
#end
As per the answers to this question, stringByAddingPercentEscapesUsingEncoding: doesn't perform a full escape encoding. For whatever reason, the CoreFoundation version of this method does, however:
[(NSString *) CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)[[self mutableCopy] autorelease], NULL,
CFSTR("=,!$&'()*+;#?\n\"<>#\t :/"), kCFStringEncodingUTF8) autorelease];
You can also use NSMutableString's replaceOccurencesOfString:withString:options: method to do the replacement manually, but that method is more repetitive and verbose. (See here.)