NSURL parameterString with .NET service call - iphone

I'm tring to use the NSURL class's parameterString message to return the string from the following url:
http://www.host.com/Api/Service.svc/GetStuff?param=thisIsTheParamValue
I keep getting nil for the parameterString. Does that url not conform to the standards that Apple uses?

The URL doesn't conform to the standard NSURL uses for parameterString. NSURL uses RFC 1808 standard, which defines the parameter string as ";" params. Use the query message instead to get the string after the '?'.

Related

How to decode special characters in an NSURL in Swift?

I have a string: <api address>/<parameters>?fmt=json
When I create an NSURL object from the string, the URL is: <api address>/<parameters>%3Ffmt=json
When I send the request with this URL (using an NSURLSession), however, it gets denied access, as the API wants the ? and doesn't accept %3F. How do I decode the URL to have the ? return so the API will accept the request?
UPDATE
NSURL construction
let urlRequestString = "<api url>?fmt=json&api_key=" + <key>
let apiRequestURL = NSURL(fileURLWithPath: urlRequestString)
You're using the wrong initializer, which is causing bogus results. NSURL(fileURLWithPath: urlRequestString) is intended for creating file:// URLs. It translates your URL into a format suitable for the filesystem. It's designed for file use only, so you shouldn't use it here.
If you change the initializer to be NSURL(string: urlRequestString) then your URL should be OK.

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];

I wanna put an xml file content into a URL, can I?

NSURL *url = [NSURL URLWithString:#"appName://<?xml version=\"1.0\" encoding=\"UTF-8\"?>"];
But the url = nil. I found if I remove the "<" and ">" signs, it will be ok.
So, the two signs cannot be used in a URL? Should I replace the signs with another one?
Thanks!
I solved this problem.
Before I do NSURL *url = [NSURL URLWithString:#"...."];
I first do:
NSString *urlStr = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
And when parse this url on the receiver side, I firstly do:
NSString *urlString = [[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // url is a NSURL
Why would you not just POST the document in the body of the request?
GET requests are not designed for this. You may get into difficulty if there are UTF-8 chars in the document.
Also What is the maximum length of a URL in different browsers? implies a max of 2083 chars.
You're into a very browser/proxy dependent scenario.
Theoretically yes. You can use urlEncode to do this.
The HTTP protocol does not place any limit on the length of the URL.
RFC says:
Note: Servers ought to be cautious about depending on URI lengths
above 255 bytes, because some older client or proxy implementations
might not properly support these lengths.

NSString Converting # to %40 on Request

Currently I'm using Resty (http://projects.lukeredpath.co.uk/resty/) to build my own API wrapper for Objective-C.
I digress however, in the request itself, I am able to determine what is actually being sent and it seems that the NSString *email (I scan for an # to make sure it is a legitimate email before storing in another NSString) is actually changed.
An example, joe#example.com has been scanned for the #, and then sent to the NSString *email for the request. However, when I send the actual GET request, the NSString is changed to joe%40example.com.
Is this due to the Resty request, or do I have to encode/decode the NSString before the request?
Yes, you should URL decode and then perform any validation tasks.
Here is what their documentation has:
NSData or any other data-encodable
payloads set the request body as-is;
encodable payloads will be encoded
using UTF8
http://projects.lukeredpath.co.uk/resty/documentation.html

URLstring class for iPhone

I am using the NSURL class in the iPhone framework foundation library to create a url request to a server. For some reason, I am getting back a nil for only this particular URL.
I don't understand why. Any reason?
The Objective C code is :
NSURL *scriptURL= [NSURL URLWithString:scriptURLString];
where scriptURLString is an NSString with value give below:
http://myserver/cgi-bin/AdsERsubset.cgi?user=139&fieldList=employee_num,mbox_num,last_name,first_name,disp_emp_num,company_num,TECH_PRIVILEDGE,printer_code,desktop_cos,id,supv_emp_num&action=first&searchkey=1 with proxy=DIRECT
The above URL is a CGI request.
Is there a special character I need to escape here?
Please advice. Thanks in advance.
The spaces in the searchkey should be escaped.
Use stringByAddingPercentEscapesUsingEncoding on the url string before passing it to URLWithString:
NSURL *scriptURL= [NSURL URLWithString:[scriptURLString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
...&searchkey=1 with proxy=DIRECT
Your missing an & aren't you?
I'd imagine it would be more like:
...&searchkey=1&with%20proxy=DIRECT
or
...&searchkey=1&withproxy=DIRECT