How to send a float value using ASIHTTPRequest? - iphone

I'm pretty new to iPhone. I want to send a request to a server with a float value for a specific key. Can anyone help me out here how to send a float value using ASIHTTPRequest in the request body. I tried changing the value to NSNumber and send it in body but the server response was an error invalid value type.
[_request setPostValue:[NSNumber numberWithFloat:y] forKey:#"Y"];
Also, I can't send float directly as an NSObject.

you can convert your float value to nsstring and then send that value.
NSString *string=[NSString stringWithFormat:#"%f",y];
[_request setPostValue:string forKey:#"Y"];

This completely depends on your server, and you haven't described that to us at all.
I can guess that the server is probably expecting a string, not an NSNumber. Try changing the code to something more like this:
[_request setPostValue:[[NSNumber numberWithFloat:y] description] forKey:#"Y"];
This might print out the value as a string. (I haven't tested it.) If not, you'll need to use NSNumberFormatter.

Related

AFNetworking 2.0 parameter dictionary not working on GET request

I'm using AFNetworking 2.0 for a normal GET request and the parameter dictionary seems to be having no effect. I'm using the standard:
[manager GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {...
but my NSMutableDictionary (parameters) doesn't seem to have any effect on the URL sent in the GET request (GET requests don't have HTTP Bodies as far as I'm aware so they won't be there). So where are the parameters going? I know I could always amend the string, but the way in which I receive the parameters on an optional basis makes it far easier to just send in a dictionary. What's wrong, why isn't it working?
Thanks,
Mike
UPDATE: I'm setting the parameter like this: [parameters setValue:self.Object.ID forKey:#"Filter.Id"]; ID is a string. However, when I do [parameters valueForKey:#"Filter.Id"] I get a (null) in the NSLog. I'm going to mess around with it a little and see how it goes, but if you have any ideas why this is happening I'd appreciate it. Also, parameters is an NSMutableDictionary and is a property of the ViewController.
Was an issue with my NSMutableDictionary property. Changed it to a global variable and everything worked fine!
Thanks,Mike

Decode REST Url in Objective-C

I'm creating a module that receive and REST URL and need to match a pattern and extract the parameters
For example:
the URL "http://Product/1" should match the pattern "http://Product/{productId:long}"
and return the Dictionary with productId as a key and "1" as the value in as long
Does anyone knows about a Framework for IPhone that does it, or at least some of it?
NSURL has a method pathComponents, which returns an array with all the different path components. That should help you get the integer part. To get the name I'd use the host method of the NSURL. The docs say, that it should work if the URL is properly formatted, might as well give it a try then.
All in all, no need to convert into a string, there seems to be plenty of methods to work out the components of the URL from the NSURL object itself.
NSString *path = [[#"path+with+spaces"
stringByReplacingOccurrencesOfString:#"+" withString:#" "]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

converting JSON object to nsdictionary from nsstring on iphone

We are trying to convert the JSON object from NSSTRING TO NSDICTIONARY but we are not getting any value. Here is the url for our webservice http://ecreeds.info/theappsoluteguideto/appservice.asmx/appArea
any help would be appreciated.
Use JSONLint.com it appears your webservice returns a 500 error for the response hence why you are not seing anything, because of invalid json
Your JSON isn't valid. In fact, it's quite off.

objective c http query

Hey, Iam new to objective c and really dont know much about it. I have been given a query that is gonna I need to send data in to the server.Query is like this http://abc.com/insertipademail.php?name=name&email=email I need to enter the name and email I have constructed the string But I dont know how to send it to the server. Can someone help me out please. Or point me in the right direction. Thanks
For starters, take a look a NSString's stringWithContentsOfURL:encoding:error: method. You could do something like:
// NSString * myURLString = whatever you do to build the url
NSURL * myURL = [NSURL URLWithString: myURLString];
NSString * response = [NSString stringWithContentsOfURL: myURL encoding: NSUTF8StringEncoding error: NULL];
NSLog(#"The response was: %#", response);
As written, this will ignore all errors and perform the request synchronously. In a real app, you probably want to handle any error that occur, and perhaps perform the request in the background. See the URL Loading System Programming Guide for further documentation. You can also try using any of several open source libraries such as those suggested in David M.'s answer.
I like the library ASIHTTPRequest. There is also HTTPRiot.

iPhone URL Request: Add value to HTTP header field

I'm trying to add a value to the header for a URL request.
Something like this works just fine:
[urlRequest addValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"];
But this doesn't even show up in the header:
NSString *authString = [[NSString alloc] initWithString:
[defaults objectForKey:#"auth"]];
[urlRequest addValue:authString forHTTPHeaderField:#"iphoneID"];
I'm completely stumped. The auth string is around 90 characters long. Is this a problem?
Edit:
Here's the code I'm trying:
NSString *authString = [[NSString alloc] initWithString:[defaults objectForKey:#"auth"]];
[urlRequest addValue:authString forHTTPHeaderField:#"iphoneid"];
[urlRequest addValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"];
I can see the Accept-Encoding header being sent through Wireshark, but iphoneid is nowhere to be found. It's just a string, 80-90 characters long.
Another Update:
So it seems that the problem isn't the field "iphoneid" but rather the authString I'm trying to pass into it. Other strings that I just create with the #"something" work fine, but the auth string that I pull from NSUserDefaults doesn't appear.
Suggestions on how to debug this?
The true problem.
The string I was pulling from NSUserDefaults already had a line ending. When set as a header, another \r\n is appended, which apparently isn't valid. Thus, the header wouldn't appear in any outgoing packets.
The fix:
Use this to trim off the characters before setting as a header value.
[yourString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
Testing checklist:
Verify that you actually have a NSMutableURLRequest (and not a NSURLRequest) at this point. In particular, check your logs for an exception due to "unrecognized selector."
Verify that urlRequest is not nil.
Switch to setValue:forHTTPHeaderField: rather than addValue:forHTTPHeaderField:.
Swap the forHTTPHeaderField: value to #"Accept-Encoding" to see if the field is the problem
Swap #"gzip" for auth to see if the value is the problem.
You need Charles web proxy, to see what header values are really outbound and inbound. Then you can see if the problem is really in your code or some magic on the server discarding things.
There's a free trial, and after you install it if you hit record any traffic the simulator sends will go through the proxy. Very nice.
http://www.charlesproxy.com/