cURL web services from iphone - iphone

I need to fetch data from curl web services from iOS, the client provided me a header name, header value and a base url, I dont know what to do with them, the url isn't giving me anything opening in a browser. They mentioned data is JSON encoded.
Please link me to some library or tutorial on how to call them.

You can use ASIHTTPRequest for cURL as well.
what I have used is:
NSURL *url = [NSURL URLWithString:url];
__weak ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
request.shouldPresentCredentialsBeforeChallenge = YES;
[request appendPostData:[JSONString dataUsingEncoding:NSUTF8StringEncoding]];
[request setUsername:username];
[request setPassword:password];
[request setRequestMethod:#"PUT"];
request.timeOutSeconds = 30;
request.validatesSecureCertificate = NO;
[request startAsynchronous];

solved it, had to set NSURLRequest http method and header like
[request setHTTPMethod:#"GET"];
[request setValue:#"HEADER_VALUE" forHTTPHeaderField:#"HEADE_RNAME_"];

Related

How to post xml data in url as request?

I am developing an application in which i want to post
xml data as request but i am not able to post it correctly ,i think.
My request xml data is
<loginRequest><username>101</username></loginRequest>
and my request is as follows :
`NSString *post=#"<loginRequest><username>101</username></loginRequest>";
NSURL *url = [NSURL URLWithString:#"my url"];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
NSMutableData *mutData=[[NSMutableData alloc]init];
[request addRequestHeader:#"Content-Type" value:#"text/xml"];
[request setPostValue:#"test" forKey:#"body"];
[request setCompletionBlock:^{
NSData *data=[request responseData];
NSString *response=[request responseString];
}];
[request setFailedBlock:^{
NSLog(#"Failed");
}];
[request startAsynchronous];
`
Kindly help me with this..
You can check with your server api whether it supports different response type..
Accordingly you can set "Accept" parameter of HTTP request header.
[request addRequestHeader:#"Accept" value:#"application/xml"];

Log into website with ASIHttpRequest

I have a website that i want to log into from my iphone in a iphone app i am making, i followed the documentation on ASIHttpRequests website but all i get from the response string is the html code for the login page , but i get a OK http code, Why is this happening?
Here is my code :
-(IBAction)fetchData:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.rssit.site90.com/login.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request shouldPresentCredentialsBeforeChallenge];
[request setRequestMethod:#"POST"];
[request setPostValue:#"" forKey:#"username"];
[request setPostValue:#"" forKey:#"password"];
[request setDelegate:self];
[request startAsynchronous];
//Add finish or failed selector
[request setDidFinishSelector:#selector(requestLoginFinished:)];
[request setDidFailSelector:#selector(requestLoginFailed:)];
}
- (void)requestLoginFinished:(ASIHTTPRequest *)request
{
NSString *yourResponse = [request responseString];
NSLog(#"%#", yourResponse);
}
The form on that page is doing a get, not a post. Your server is probably not expecting POST data, and is looking at query string params instead. Change to
[NSURL URLWithString:#"http://www.rssit.site90.com/login.php?username=YOURUSERNAME&password=YOURPASSWORD"];
and set the requestMethod to GET.

JSON touch iphone-sdk, send data over request

people,
I'm using JSON touch inmy iphone app.
Now I have to send a string and then an array to server, how can I do this?
I get data from json requests succefully, but I have to send some data.
Here is the code I've got so far:
-(void)subimtSelection:(int)aNumber
{
NSString *choiceData=[NSString stringWithFormat:#"%d", aNumber];
NSError *theError=nil;
[[CJSONSerializer serializer] serializeString:choiceData error:&theError];
NSDictionary *jsDic=[NSDictionary dictionaryWithObject:choiceData
forKey:#"selection"];
//WHAT SHOULD I DO NEXT?
}
You can use ASIHTTRequest to POST string/json data to server:
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:#"http://server.url"];
[request addRequestHeader:#"Accept" value:#"application/json"];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
[request setRequestMethod:#"POST"];
[request appendPostData:[yourJSONString dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
If you want to post a string value then try:
[request appendPostData:#"key=value"];
ASIHTTPRequest can be used in asynchronious mode as well.
P.S. I did not tested the code, but it should work.
To post form data you can use ASIFormDataRequest, documentation is here

How do I use ASI Http in iOS to POST data to a web service?

I am using the instructions provided in the ASI page here. I am trying to send some data to a web service and not seeing any results.
This is my sendRequest method which gets called in viewDidLoad
-(void)sendRequest {
NSURL *url = [NSURL URLWithString:#"http://153.60.6.75:8080/BarcodePayment/transactions"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:#"Accept" value:#"application/json"];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
NSString *dataContent = #"{\"id\":7,\"amount\":7.0,\"paid\":true}";
NSLog(#"dataContent: %#", dataContent);
[request appendPostData:[dataContent dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];
}
I check the dataContent string and the output is
{"id":7,"amount":7.0,"paid":true}
If I use curl from Terminal, I checked and this command works.
curl -X POST -H 'Accept:application/json' -H 'Content-Type: application/json' http://153.60.6.75:8080/BarcodePayment/transactions/ --data '{"id":7,"amount":7.0,"paid":true}'
My understanding is that in using curl, I set it to json, specify the address, specify the data which is equivalent to dataContent in my code. Nothing happens. What's wrong?
Thanks for the help!
You have pretty much everything except the most crucial component which is to start the request
You need to add [request startSynchronous]; for a Synchronous Request or [requester startAsynchronous]; for a Asynchronous request (and you possibly need the Delegate Methods to handle any response you have back)
This is all covered pretty nicely in the ASIHTTPRequest How to Use Guide. The Sections most relevant to this would be 'Creating a synchronous request' and 'Creating an asynchronous request'. Also something to think about taken from that page:
In general, you should use asynchronous requests in preference to synchronous requests. When you use ASIHTTPRequest synchronously from the main thread, your application’s user interface will lock up and become unusable for the duration of the request.
You forgot to call:
[request setDelegate: self];
[request startAsynchronous];
Or:
[request startSynchronous];
If you don't call any of these, the request will never be made :)
I don't see a call to [request startSynchronous] (or [request startAsynchronous]) in your code... Are you even initiating the request anywhere?
It looks like you didn't call [request startAsynchronous];
Check ASIHTTPRequest documentation for more details on how to use
You're not actually sending the request.
1) You'll need to call [request startSynchronous] or [request startAsynchronous]
2) If you use asynchronous (which you should probably do) you'll need to set the delegate and implement a - (void)requestFinished:(ASIHTTPRequest *)request method.
- (void)postThresholdDetails:(NSDictionary *)info
{
NSString *urlString = [NSString stringWithFormat:#"%#%#/",BaseUrl,PostThresholdDetails];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:120];
[request addRequestHeader:#"Accept" value:#"application/json"];
[request addRequestHeader:#"content-type" value:#"application/x-www-form-urlencoded"];
request.allowCompressedResponse = NO;
request.useCookiePersistence = NO;
request.shouldCompressRequestBody = NO;
[request setPostBody:[NSMutableData dataWithData: [info objectForKey:#"jsondata"] ]];
[request startAsynchronous];
}

Post request from iPhone using ASIFormDataRequest not working

Newbie to Rails/iOS here. I have a rails blog application. I'm trying to upload a post from iOS using an HTTP POST method with ASIFormDataRequest. Here's the code that get's called:
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"name"];
[request setFile:#"star.png" forKey:#"image"];
[request startSynchronous];
NSString *response = [request responseString];
NSLog(#"response:: %#", response);
When I run this code, nothing happens. My server does not get contacted. I get response:: (null). Any idea why?
EDIT I found out that star.png needed to have its full file address. Now the POST works fine, but neither the name or image get saved into the db. A new post with empty name and image gets created. Any idea why?
why you are using localhost here?
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
use your web server ip instead of localhost
NSURL *url=[[NSURL alloc] initWithString:#"http://yourservername:3000/posts"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"name"];
[request setPostValue:imageData forKey:#"image"];
[request startSynchronous];
For the "imagePath" is the path to your image. The full path.