How to use XMLHttpRequest in Objective-C? - objective-c

I'm creating easy to use kannada comic reader on mac for my cousins. I'm able to pull the comic pages, but lot of meta content exchanged webpage and server using XMLHttpRequest.
I'm finding difficulties use XMLHttpRequest, could anyone tell me how can I use it?

The closest thing to a simple XMLHttpRequest is to create a data object from a URL.
For more advanced requests (e.g., custom headers, request methods that aren't GET), you'll need to use NSURLConnection. You should probably do that anyway; your application will be more robust and more responsive for it.

Related

What is the main difference between HTTP request to a website VS HTTP request to the REST API?

I have been trying to wrap my head around this and I'd appreciate if someone could enlighten me.
What is the main difference between making a "HTTP GET" request to a webpage and fetching the HTML VS making "HTTP GET" request to a REST API endpoint and fetching JSON?
As far as I know, REST API can also return HTML, so in that case, what would be the main differentiator between these two?
I may be missing something obvious here but I am just trying to conceptualize each action so I have no more ambiguity.
Thanks in advance.
What is the main difference between making a "HTTP GET" request to a webpage and fetching the HTML VS making "HTTP GET" request to a REST API endpoint and fetching JSON?
Not much?
In the REST architectural style (which is followed by HTTP... mostly). an important constraint is uniform interface, which includes the idea that all resources understand all messages the same way.
That means we can do things like use a general purpose web browser to fetch documents, or a general purpose cache to store representations, without needing any special code to distinguish "web resources" from "api resources".
A "good" REST API looks like a machine readable web site.
The only difference that you might notice in some cases is that the hypermedia representations of resources might use some standardized format that isn't HTML.
Otherwise, they are the same thing - the main difference being that a "REST API" tends to have more resources that are expected to be changed by incoming requests, as opposed to a website which is designed more for reading than editing. So PUT/POST/PATCH requests are more commonly used with API resources.

can not set restricted headers e.g. UserAgent in WebRequest for PCL

microsoft did not implement it, and will not do so according to this:
https://connect.microsoft.com/VisualStudio/feedback/details/770104/cannot-set-useragent-of-a-httpwebrequest-in-portable-class-and-winrt-libraries
But the web site I'm trying to get response from requires User-Agent header specified in the request, otherwise the response is wrong.
I am limited to using portable class library (PCL), since I'm working on mvvmcross cross platform project.
What is the best way to make such a web request?
As I was trying to point you to in my first comment, Dependency Injection is the best way to go about this. Here is a simple example from the MvvmCross documentation https://github.com/MvvmCross/MvvmCross/wiki/Service-Location-and-Inversion-of-Control#1-pcl-interface-with-platform-specific-implementation

'Dynamic' page loading?

I've no idea what it is exaclty I need, but I'll use Facebook as an example. When you load another page (for example: on the home page, you click your profile) the page itself doesn't look like it reloads.The data loads of course, and the URL changes, however the topbar and chat bar stay put.
I'm looking to do something similar to this. Is this me assuming too much and it's really a simple cache function, or is there a JavaScript way to do it, or any other?
Thanks.
It's using AJAX, which is a combination of various technologies (primarily JavaScript) to achieve this dynamic client-server interaction within the overall context of a page.
Essentially what you'd have for your setup is something like this:
A page which the user requests and is returned to the browser. This page will contain (or reference in external files, etc.) some JavaScript code to drive the interactive functionality. For example, a "link" may trigger a JavaScript function instead of navigate to another page. That function will make an AJAX call to the server.
An AJAX handler on the server. Think of it like another page, or like a web service of some kind. It expects requests to come from JavaScript code, not from humans. (Though humans can call it manually if they want, so don't return sensitive data or anything like that.) This can return data in any number of formats (JSON, XML, HTML, etc.) and the client-side JavaScript code will use that response in code.
Depending on your preferred web development technologies, there are various frameworks and tools to help with AJAX functionality. My personal favorite is just to use the AJAX methods in jQuery and to manually control the responses from the server. If you specify your development platform, I can help find some useful examples to get you started.
This is all done using AJAX. Depending on what you want, you can load entire HTML chunks, or just load the data and have the page's javascript output what's needed.

why do we need addRequestHeader method?

Can anyone explain me the role of addRequestHeader method of ASIHTTPRequest class? Why do we need it?
P.S. Browsing over google and stackoverflow i see lots of people familiar with details of ASIHTTPrequest library. Where do you take the information?
Without any knowledge about that class, but most likely the method is used to send a custom header.
For example, when accessing a web service that is usually only hit through XHRs from JavaScript, it might require you to add a X-Requested-With: XMLHttpRequest header.
The info is from ASIHTTPRequest documentation
You need the method to specify header of request.
List of HTTP header fields

goo.gl shortening api: shorten via GET request

Is it possible to shorten a URL using the Goo.gl shortening api with a GET request? Their only instructions are for POST and it doesn't make much sense that they wouldn't have a way to do this via GET.
It's actually unlikely that they support GET to do that. Good practice requires that GET requests not cause side effects (permanent data changes) in web applications. This prevents problems related to web spiders causing havoc simply by trying to crawl a site (imagine a "delete" button that worked with a GET, causing a spider to inadvertently remove content).
Additionally, GET requests are a lot easier to force a third party to do (i.e. embed the url in an image tag on a forum) which often is a security problem. In the case of goo.gl, it would allow trivial and hard to block DoS type attacks on the service.