NSHTTPCookie cookiesWithResponseHeaderFields - swift2

When making a call to an API, I receive this as response:
<NSHTTPURLResponse: 0x7f96f0510f80> { URL: https://insula.magister.net/api/sessie } { status code: 201, headers {
"Cache-Control" = "max-age=120, private";
"Content-Length" = 0;
Date = "Thu, 30 Jul 2015 17:27:11 GMT";
"Set-Cookie" = "SESSION_ID=c8714acb-22f8-4295-90ee-82e9469890e1; domain=insula.magister.net; path=/api; secure; httponly";
"Strict-Transport-Security" = "max-age=31536000";
Vary = "Accept-Encoding,Cookie";
"X-Frame-Options" = DENY;
} }
print(HTTPResponse.allHeaderFiles) prints the following output:
[Set-Cookie: SESSION_ID=65a59001-de75-41ff-ad22-70549baa96f5; domain=insula.magister.net; path=/api; secure; httponly, Strict-Transport-Security: max-age=31536000, Date: Thu, 30 Jul 2015 17:33:13 GMT, X-Frame-Options: DENY, Content-Length: 0, Cache-Control: max-age=120, private, Vary: Accept-Encoding,Cookie]
But I can't get cookiesWithResponseHeaderFields to work, I should pass a [String:String] array as first parameter, but the response doesn't seem to meet.
let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(HTTPResponse.allHeaderFields, forURL: url)
But as HTTPResponse.allHeaderFiles is an array of type [NSObject:AnyObject] it can't meet the [String:String] parameter type.
Who knows what to do?

There's a similar question with a great answer.
You should cast the allHeaderFields property to the type you need, that is [String:String], like this:
let foundCookies: [NSHTTPCookie]?
if let responseHeaders = HTTPResponse.allHeaderFields as? [String:String] {
foundCookies = NSHTTPCookie.cookiesWithResponseHeaderFields(responseHeaders, forURL:url)
}

Related

Zend Http Client Reponse header data?

I get json file content using the zend http client in magento.
I don't need header info on getBody(). I did test it on some host where it works. But the live host gives me troubles.
$request_url = "link";
$httpClientConfig = array('maxredirects' => 0);
$client = new Zend_Http_Client($request_url, $httpClientConfig);
$client->setMethod(Zend_Http_Client::GET);
try {
$response = $client->request();
} catch (Exception $e) {
Mage::throwException($this->__('Gateway request error: %s', $e->getMessage()));
}
Mage::log($response->getBody());
Result logging:
2015-01-08T09:12:46+00:00 DEBUG (7): HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 761
Connection: close
Date: Mon, 05 Jan 2015 22:53:40 GMT
Last-Modified: Sat, 15 Nov 2014 10:14:17 GMT
ETag: "a8b17a42b7ef7e5960f9bd325a8c1892"
Accept-Ranges: bytes
Server: AmazonS3
Age: 5574
X-Cache: Hit from cloudfront
Via: 1.1 522dd06c4c8acf822ccbebe21aee8d1c.cloudfront.net (CloudFront)
X-Amz-Cf-Id: c6K9QJnOESg1NERKjG-v2fX_9eskmCzz_KUYdXrOb2NSjVTbWZ_x8Q==
{
"assets": {
"standard": {
"url": "https://d3k1w8lx8mqizo.cloudfront.net/standard.png",
"infobox": {
"page1": "https://d3k1w8lx8mqizo.cloudfront.net/frontside.png",
"page2": "https://d3k1w8lx8mqizo.cloudfront.net/backside.png"
}
},
"promotion": {
"infobox": {
"page1": "https://d3k1w8lx8mqizo.cloudfront.net/frontside.png",
"page2": "https://d3k1w8lx8mqizo.cloudfront.net/backside.png"
},
"url": "https://d3k1w8lx8mqizo.cloudfront.net/standard.png",
"interest_free_months": 6,
"transaction_limit_min": 240.0
}
}
}
Try this:
Zend_Http_Response::extractBody($response->getBody());
Or don't retrieve the headers in the request when instantiating the Zend_Http_Client:
$httpClientConfig = array(
'maxredirects' => 0,
'curloptions' => array(CURLOPT_HEADER => false),
);
If the above doesn't work, you can try an alternative way of doing the request such as file_get_contents:
$response = file_get_contents($request_url);
I hope it helps.

Parse.com Analytics not showing

I'm trying to connect to Parse.com 's REST-API via NSURLConnection to track AppOpened metadata.
I get 200 OK back from the API and the headers are the same to the cURL headers but my API calls are not being represented in the data browser on Parse.com . Is NSURLConnection doing something silly I don't know of? API response is the same but one request gets represented while the other one isn't.
NSLog output:
<NSHTTPURLResponse: 0x7ff5eb331ca0> { URL: https://api.parse.com/1/events/AppOpened } { status code: 200, headers {
"Access-Control-Allow-Methods" = "*";
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Length" = 3;
"Content-Type" = "application/json; charset=utf-8";
Date = "Sun, 04 Jan 2015 22:42:54 GMT";
Server = "nginx/1.6.0";
"X-Parse-Platform" = G1;
"X-Runtime" = "0.019842";
} }
cURL output:
HTTP/1.1 200 OK
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Date: Sun, 04 Jan 2015 23:03:51 GMT
Server: nginx/1.6.0
X-Parse-Platform: G1
X-Runtime: 0.012325
Content-Length: 3
Connection: keep-alive
{}
It's the same output. What am I doing wrong? Has anyone experience with this?
Turns out Parse was showing funny API keys the moment I copied them out of the cURL example they provide in their lovely docs. Don't know whose analytics I screwed over but I'm terribly sorry and it wasn't my fault!
Always copy your API keys out of [Your-Parse-App-Name]->Settings->Keys
It probably was just a stupid glitch that happened on the Server.

HtmlUnit does not return all headers

final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage(webPageURL);
final String pageAsXml = page.asXml();
final String pageAsText = page.asText();
List <NameValuePair> response = page.getWebResponse().getResponseHeaders();
for (NameValuePair header : response) {
log.info(header.toString() + " = " + header.getValue());
The web page returns more than one header. But the log shows only the first header. How do I get rest of headers? The header I am looking for is of Content Type: application/javascript;charset=ISO-8859-1
The web page is an internal web page.
The code you provided is working for me. Actually, I replaced the header.toString() for header.getName():
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://www.debian.org");
List<NameValuePair> response = page.getWebResponse().getResponseHeaders();
for (NameValuePair header : response) {
System.out.println(header.getName() + " = " + header.getValue());
}
The output is:
Date = Thu, 22 Aug 2013 19:48:54 GMT
Server = Apache
Content-Location = index.en.html
Vary = negotiate,accept-language,Accept-Encoding
TCN = choice
Last-Modified = Thu, 22 Aug 2013 15:31:17 GMT
ETag = "3887-4e48afb257b40"
Accept-Ranges = bytes
Cache-Control = max-age=86400
Expires = Fri, 23 Aug 2013 19:48:54 GMT
Content-Encoding = gzip
Content-Length = 4605
Keep-Alive = timeout=15, max=100
Connection = Keep-Alive
Content-Type = text/html
Content-Language = en
As you can see, the Content-Type header is there. Can you confirm the server is actually sending that piece of data (it is a common header so it should).

CouchDB: Get server-time with an http request in CouchApp

I need the server-time for "user-is-online" stats in my CouchApp. I work with jquery.couch.js and would prefer to have a url, e.g. /db/_design/app/time - which gets me a timestamp.
How do I realize this?
A show function could do that:
function(doc, req) {
// _design/myapp/_show/now
// First version possibly incompatible with some spidermonkey versions.
//var now = new Date();
var now = new Date().getTime();
var output = JSON.parse(JSON.stringify(now)) + "\n";
return { code: 200
, headers: { "Content-Type": "text/plain"
}
, body:output
};
}
The server also includes a Date header that you might want to use.
$ curl -D- http://localhost:5984
HTTP/1.1 200 OK
Server: CouchDB/1.1.0 (Erlang OTP/R14B)
Date: Fri, 27 May 2011 00:28:31 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 40
Cache-Control: must-revalidate
{"couchdb":"Welcome","version":"1.1.0"}

Why do I get two redirectResponses when receiving one 302 response?

I use the following code:
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {
NSLog(#"Received redirect Response: %# %#", [response allHeaderFields], [NSHTTPURLResponse localizedStringForStatusCode:[response statusCode]]);
return request;
}
When I receive a 302 with the following header data:
< HTTP/1.1 302 Found
< Date: Wed, 03 Mar 2010 07:47:17 GMT
< Server: lighttpd/1.4.19
< Content-length: 0
< Content-type: text/html;charset=utf-8
< Location: `<new Location>`
< Vary: Accept-Encoding
this is the output in gdb console:
2010-03-03 08:42:03.265 MyProg[68106:207] Received redirect Response:
(null) server error 2010-03-03 08:42:14.414 MyProg[68106:207]
Received redirect Response: {
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Length" = 20;
"Content-Type" = "text/html;charset=utf-8";
Date = "Wed, 03 Mar 2010 07:42:10 GMT";
"Keep-Alive" = "timeout=15, max=100";
Location = "<new Location>";
Server = "lighttpd/1.4.19";
Vary = "Accept-Encoding"; } found
When using Curl I only get one response and tracedump tells the same, so I am sure that the server sends only one redirect.
Why is this selector called twice?
connection:willSendRequest:redirectResponse: gets called before every request, so it is called once on the original request, which was not a redirect so response is nil; then it gets called when loading the redirection target, where response is the 302 response to the initial request.

Resources