NSURLRequest to NSString
Asked Answered
S

5

17

how can i convert NSURLRequest to NSString ?

Saez answered 6/3, 2010 at 22:13 Comment(0)
C
9

Depends on the information you want in the string. Do you want to have it contain all the values of the instance variables in the object? If so, you're going to need to write your own method that does that. Perhaps subclass NSURLRequest and override description. You could also use reflection to get at all the private ivars and print everything out from another class.

Or just use a debugger to inspect the values.

Collimator answered 6/3, 2010 at 22:20 Comment(4)
tnx dude...but i'm new to cocoa... in my code : NSURLRequest *newRequest=request; NSLog(@"%@",newRequest);// <NSURLRequest world.yahoo.com> i need put "<NSURLRequest world.yahoo.com>" in to the String variable.Saez
NSLog(@"<NSURLRequest %@>", [[newRequest URL] absoluteString]); Take a look a the API docs for NSURLRequest: developer.apple.com/mac/library/documentation/cocoa/reference/…Collimator
tnx Marc; NSString *naeim = [[newRequest URL] absoluteString]; this command was true ;) .Saez
It's kind of a silly question, but this is also a silly answer. It's pretty clear the user was asking for the URL as a string, which is why the comment above is getting so many upvotes.Dawes
C
48
- (NSString*) urlRequestToString:(NSURLRequest*)urlRequest
{
  NSString *requestPath = [[urlRequest URL] absoluteString];
  return requestPath;
}
Clydesdale answered 4/8, 2011 at 8:2 Comment(2)
This only prints the URL of the NSURLRequest. That isn't the only thing that is important on the NSURLRequest.Mariandi
@Mariandi - Clearly. It's also clearly what the OP was after.Dawes
C
9

Depends on the information you want in the string. Do you want to have it contain all the values of the instance variables in the object? If so, you're going to need to write your own method that does that. Perhaps subclass NSURLRequest and override description. You could also use reflection to get at all the private ivars and print everything out from another class.

Or just use a debugger to inspect the values.

Collimator answered 6/3, 2010 at 22:20 Comment(4)
tnx dude...but i'm new to cocoa... in my code : NSURLRequest *newRequest=request; NSLog(@"%@",newRequest);// <NSURLRequest world.yahoo.com> i need put "<NSURLRequest world.yahoo.com>" in to the String variable.Saez
NSLog(@"<NSURLRequest %@>", [[newRequest URL] absoluteString]); Take a look a the API docs for NSURLRequest: developer.apple.com/mac/library/documentation/cocoa/reference/…Collimator
tnx Marc; NSString *naeim = [[newRequest URL] absoluteString]; this command was true ;) .Saez
It's kind of a silly question, but this is also a silly answer. It's pretty clear the user was asking for the URL as a string, which is why the comment above is getting so many upvotes.Dawes
D
3

If you're using swift, you can implement the following extension:

extension NSURLRequest: DebugPrintable {
    public func extendedDescription() -> String {

        var result = "<\(NSStringFromClass(self.dynamicType)): " + String(format: "%p", self)
        result += "; HTTPMethod=" + (HTTPMethod ?? "nil") + "; URL=\(URL); timeoutInterval=" + String(format: "%.1fs", timeoutInterval) + "> {"

        // Add header fields.
        if let headers = allHTTPHeaderFields {
            result += "\nallHTTPHeaderFields {"
            for (key, value) in headers {
                result += "\n\t\(key) : '\(value)'"
            }
            result += "\n}"
        }

        if let body = HTTPBody {
            result += "\nHTTPBody {\n " + ((NSString(data: body, encoding: NSASCIIStringEncoding) ?? "") as String) + "}"
        }

        return result + "\n}"
    }

    public override var debugDescription: String {
        return extendedDescription()
    }

    public override var description: String {
        return extendedDescription()
    }
}

Or check out this gist: https://gist.github.com/tomaskraina/d644af49e4968de85e34

If printed out, it displays something similar to the following:

<NSMutableURLRequest: 0x7fa91977e390; HTTPMethod=POST; URL=https://myapi.com/api/something; timeoutInterval=300.0s> {
allHTTPHeaderFields {
    Accept-Encoding : 'gzip'
    Content-Type : 'application/json'
    Accept : 'application/json'
}
HTTPBody {
    {"key1":"value1"}
}
}
Dupont answered 21/4, 2015 at 13:0 Comment(1)
Thanks, really helped me debug an objective c call I was migrating to swift.Saga
H
1
NSURLRequest *urlRequest;
...
NSLog(@"%@", [urlRequest allHTTPHeaderFields]);
NSLog(@"%@",[urlRequest valueForHTTPHeaderField:field]);

In this example, urlRequest is an NSURLrequest (from the previous example above). The second line allows you to print the value of a specific field 'field' that has been added to the NSURLRequest.

From Apple docs

- (NSString *)valueForHTTPHeaderField:(NSString *)field Parameters

field

The name of the header field whose value is to be returned. In keeping with the HTTP RFC, HTTP header field names are case-insensitive.

Return Value

The value associated with the header field field, or nil if there is no corresponding header field.

Hexarchy answered 6/3, 2014 at 11:49 Comment(7)
It what why does this help answer the question??? And how does this add to the other answers?? Seem like a pointless answer and doesn't help anyone -1Callable
@Callable - it shows another method method of getting the NSURLRequest as a string, exactly as per the original question and not one provided by any other answers on the page. How in the world is that pointless?Hexarchy
Well it doesn't though. Since one none of us know what the variable urlRequest is, it could be anything (Obvious one would be NSURLRequest but a newbie might not know that) and what is field? Since HTTPHeaderFields have quite a few fields in there this could actually be absolutely anything. So this doesn't actually help solve the problem. You need to do two things provide all the code and explain your code until then my -1 remains.Callable
urlRequest is referenced in the answer above. I merely used the same name. It's name also clearly hints that it is an NSURLRequest! I take your point about field: and will edit my answer.Hexarchy
You shouldn't use other peoples answers as reference and expect others to know what your talking about, if your going to give an answer and use use variables you need to explain what they are in your own answer. I could do NSString *urlRequest and when I do your code I would get unrecognized selector exception which would be very bad. My -1 still remains as your answer remains very unclear still.Callable
I have further clarified my answer. Please let me know if you feel this is still ambiguous in a ny way.Hexarchy
Thanks for the help and guidance, Popeye.Hexarchy
H
1

In iOS 9 / Xcode 7 you can use request.URLString in Swift (or in Objective-C, actually)

Homer answered 2/3, 2016 at 0:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.