I have following JSON for sending request on server
{
"name":"Home",
"latitude":"45.5037078163837",
"longitude":"-122.622699737549",
"radius":"240"
}
and URL of for request is
URL: https://api.geoloqi.com/1/place/create
I am making request like this,
NSString *urlString= #"https://api.geoloqi.com/1/place/create ";
NSURL* url = [[NSURL alloc] initWithString:urlString];
NSString *jsonRequest = #"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\,\"radius\":\"500\ }";
jsonRequest = [self JSONString:jsonRequest];
NSData* requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:#"%d", [requestData length]];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:requestData];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:requestDataLengthString forHTTPHeaderField:#"Content-Length"];
[request setTimeoutInterval:30.0];
[url release];
[requestData release];
[requestDataLengthString release];
NSURLConnection *m_URLConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[request release];
What is wrong with this request ?
Your JSON string seems to be missing 2 quotes after the longitude and radius values:
Change
NSString *jsonRequest = #"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\,\"radius\":\"500\ }";
To this
NSString *jsonRequest = #"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\",\"radius\":\"500\" }";
It is not clear from your question what is your issue with that.
Anyway, are you doing also:
[m_URLConnection start];
after creating the connection?
Alternatively, you could create the connection using
[– initWithRequest:delegate:startImmediately:][1]
which allows you to specify that the loading of data shall start immediately.
Also consider using [+sendAsynchronousRequest:queue:completionHandler:][2] as a convenience constructor for your connection.
The Webservice URL you are trying to hit is seem to be using SSL. You may need to put access token in request header in order to get the proper response from web service.
Please see the link for authentication:
https://developers.geoloqi.com/api/authentication
Related
I have the following details, for get the data from server. What is the use of methodIdentifier and web service name ?
{"zip":"12345","methodIdentifier":"s_dealer"}
url:- http://xxxxxxxxxxxxxxx.com/api.php
method: post
web service name: s_dealer
response : {"success":"0","dealer":[info...]}
I don't know how to send zip number "12345" with the url. Please direct me on right direction. I use the following.
-(void)IconClicked:(NSString *)zipNumber
{
NSString *post = [NSString stringWithFormat:#"&zipNumber=%#",zipNumber];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://xxxxxxxxxxxxxxxxxxx.com/api.php"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(#"Connection Successful");
}
else
{
NSLog(#"Connection could not be made");
}
receivedData = [[NSMutableData alloc]init];
}
when i print the response in console :\"Unexpected end of string\"
Without knowing more about the API, I can't be certain about your requirements, but it seems that the server is expecting the request to contain JSON. The way you currently are creating the body for the request is using standard POST variables.
Have you tried changing:
NSString *post = [NSString stringWithFormat:#"&zipNumber=%#",zipNumber];
to:
NSString *post = [NSString stringWithFormat:#"{\"zip\":\"%#\",\"methodIdentifier\":\"s_dealer\"}",zipNumber];
Regarding your other questions, I'm guessing that there is a single URL for the API. The methodidentifier is used by the server in order to determine which server method(s) to run.
You get this error because you do not get a json as a response, but an error from Apache (or whatever), that has different structure, and json cannot parse it. Try my method to initiate the connection, in order to gain a successful one.
Declare a NSURLConnection property and synthesize it. Now:
NSString *post = [NSString stringWithFormat:#"zipNumber=%#",zipNumber];
NSString *toServer = [NSString stringWithString:#"your server with the last slash character"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#api.php?", toServer]];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[urlRequest setURL:url];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[urlRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[urlRequest setValue:#"utf-8" forHTTPHeaderField:#"charset"];
[urlRequest setHTTPBody:postData];
[urlRequest setTimeoutInterval:30];
NSURLConnection *tmpConn = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
self.yourConnectionProperty = tmpConn;
Now you work with self.yourConnectionProperty in connection delegates. Cheers!
hey bro check my answer for same problem may help you... You have to use the NSURLConnection Delegates to get the data
Could not make Json Request body from Iphone
I'm pretty new to everything that deals with http and stuff like that and i know almost nothing about how http methods work so i need your advice. In my app i have to send login and password to a server and get a response. By now i'm trying to write a method (it's called STUB in English i believe) which will work in a general way without communicating to actual server. Should i use POST method in a manner similar to:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:some_server_url];
[request addPostValue:passwordString forKey:loginString];
or something else? if first how do i receive the response from the server? Could you please show me few lines of code?
You can pass login details to server using HTTP POST method like this :
-(NSDictionary*)userLogIn:(NSMutableDictionary*)inputDictionary{
SBJSON *parser = [[SBJSON alloc] init];
NSString *convertedString=[NSString stringWithFormat:#"login=%#&password=%#",username, password];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"Give the url here"]];
[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:#"XXXX" forHTTPHeaderField:#"USER_AGENT"];
[request setHTTPMethod:#"POST"];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];
NSDictionary *statuses = [parser objectWithString:json_string error:nil];
[parser release];
[json_string release];
return statuses;
}
This is a sample code and you have to change according to your requirements.
i am trying to post to a server in the following code.
i cant receive the response that i need to get.
think i am making something wrong, need some help
NSString *uuid = #"acdede9b-6fa6-44d0-8837-cd11771139be";
NSString *rec = #"ewoJInNpZ25hdHVyZSIgPSAiQWphZElyM2hFclc2L0xReDYwNGtpZ3FpZmxZUll4MTdJUUN1VUQ3UG1LS2tCNU1yeUxvU2NOejdnKzVlYnZBS001TnV2R3dOVWRmcmFEeHN3eElONVJXZXFoc1Q0OEJMUVlXNzBnWmFyTUhVbW81SW1BdFVvZy9PWlQyTkVvdW5DNW5uR3JBQll0K0Q2VFdZK1hEL0NIczczcmpqcmtONnNFalNmR05kNEg4K0FBQURWekNDQTFNd2dnSTdvQU1DQVFJQ0NHVVVrVTNaV0FTMU1BMEdDU3FHU0liM0RRRUJCUVVBTUg4eEN6QUpCZ05WQkFZVEFsVlRNUk13RVFZRFZRUUtEQXBCY0hCc1pTQkpibU11TVNZd0pBWURWUVFMREIxQmNIQnNaU0JEWlhKMGFXWnBZMkYwYVc5dUlFRjFkR2h2Y21sMGVURXpNREVHQTFVRUF3d3FRWEJ3YkdVZ2FWUjFibVZ6SUZOMGIzSmxJRU5sY25ScFptbGpZWFJwYjI0Z1FYVjBhRzl5YVhSNU1CNFhEVEE1TURZeE5USXlNRFUxTmxvWERURTBNRFl4TkRJeU1EVTFObG93WkRFak1DRUdBMVVFQXd3YVVIVnlZMmhoYzJWU1pXTmxhWEIwUTJWeWRHbG1hV05oZEdVeEd6QVpCZ05WQkFzTUVrRndjR3hsSUdsVWRXNWxjeUJUZEc5eVpURVRNQkVHQTFVRUNnd0tRWEJ3YkdVZ1NXNWpMakVMTUFrR0ExVUVCaE1DVlZNd2daOHdEUVlKS29aSWh2Y05BUUVCQlFBRGdZMEFNSUdKQW9HQkFNclJqRjJjdDRJclNkaVRDaGFJMGc4cHd2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WlcwdGFXUWlJRDBnSWpRM05USXhNVFUzTlNJN0Nna2laWGh3YVhKbGN5MWtZWFJsTFdadmNtMWhkSFJsWkNJZ1BTQWlNakF4TVMweE1TMHlPQ0F3TVRveE5Eb3dNQ0JGZEdNdlIwMVVJanNLQ1NKbGVIQnBjbVZ6TFdSaGRHVWlJRDBnSWpFek1qSTBOREk0TkRBd01EQWlPd29KSW5CeWIyUjFZM1F0YVdRaUlEMGdJbU52YlM1aGJXOTBaV05vTGs5dVpWbGxZWElpT3dvSkluUnlZVzV6WVdOMGFXOXVMV2xrSWlBOUlDSXhNREF3TURBd01ERTBPRE00TURnMUlqc0tDU0p2Y21sbmFXNWhiQzF3ZFhKamFHRnpaUzFrWVhSbElpQTlJQ0l5TURFeExURXhMVEkzSURJeU9qRXpPak16SUVWMFl5OUhUVlFpT3dvSkltOXlhV2RwYm1Gc0xYUnlZVzV6WVdOMGFXOXVMV2xrSWlBOUlDSXhNREF3TURBd01ERTBPREk1TVRJeElqc0tDU0ppYVdRaUlEMGdJbU52YlM1aGJXOTBaV05vTG1Gd2NHTnBaR1Z1ZEVGd2NDSTdDZ2tpWW5aeWN5SWdQU0FpTVM0d0lqc0tmUT09IjsKCSJlbnZpcm9ubWVudCIgPSAiU2FuZGJveCI7CgkicG9kIiA9ICIxMDAiOwoJInNpZ25pbmctc3RhdHVzIiA9ICIwIjsKfQ==";
NSString *ServerContent = [NSString stringWithFormat:#"registerRecipt.aspx?udid=%#&receipt=%#",uuid,rec];
NSData *requestdata = [ServerContent dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"myserverurl"]];
request.timeoutInterval = 20;
[request setHTTPMethod:#"POST"];
[request setHTTPBody:requestdata];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
error
The page cannot be displayed Open IIS Help, which is accessible in IIS Manager (inetmgr),
Try setting the Content-Length header:
[request setValue:[NSString stringWithFormat:#"%d", [requestData length]]
forHTTPHeaderField:#"Content-Length"];
So I am trying to log into my site using an NSMutableURLRequest which presents the credentials via a post. As a noobie, I have some questions about the functionality of this method to make sure I am understanding it.
NSString *post = [NSString stringWithFormat:#"username=%#&password=%#&TARGET=%#",LoginId,LoginPwd,target];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setHTTPShouldHandleCookies:YES];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://somelogin.mycomp.verify.fcc"]]];
//[request setURL:url];
[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];
Now this will automatically handle any redirects via the delegate method correct? It will also collect any cookies along the way? Also, should I be using an asynchronous request instead? Thanks!
UPDATE:
So I have deduced that some of the cookies are being ignored by the request and subsequent redirects. Does anyone know why that may be happening?
you can do something like this when logging in synchronously... the code is not perfect, but this is the general idea.
look here for documentation
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW1
// credentials set here
NSURLCredential *creds = [NSURLCredential credentialWithUser:LoginId password:LoginPwd
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionedSpace = [[NSURLProtectionSpace alloc]
initWithHost:#"mycomp.verify.fcc"
port:8443
protocol:#"https"
realm:nil
authenticationMethod:nil];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:creds
forProtectionSpace:protectionedSpace];
// removed credentials from line...
NSString *post = [NSString stringWithFormat:#"TARGET=%#",target];
here is a tutorial that does it asynchronously
http://iosdevelopertips.com/networking/handling-url-authentication-challenges-accessing-password-protected-servers.html
I am trying to send the data to server from my Iphone client. It works fine for most values but when itry to send a string like "IPhone+Cocoa" the server shows the string as "IPhone Cocoa". I have tried to google it but without success is there any why of doing it.
Here is my code
-(void)sendRequest:(NSString *)aRequest
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kURLRequest]];
NSString *httpBody = [NSString stringWithFormat:#"%#=%#",[requestString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[aRequest stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *aData = [httpBody dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:aData];
[request setHTTPMethod:#"POST"];
self.feedURLConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
Can anyone help me?
You do this for requestString and aRequest,but not after formatting together with your addition..
Try
httpBody = [httpBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
before
NSData *aData = [httpBody dataUsingEncoding:NSUTF8StringEncoding];