Difference between http and HttpClient in Dart - http

I'm trying to understand the difference between the http package and the HttpClient class in the dart:io library. My purpose is to known when I should use which. I see both of them used to apparently do the same thing.
I've seen these Q&As:
Choosing between package:html, dart:html, dart:io (class HttpClient) and package:http APIs to fetch HTTP resources
How to make HTTPS request using HttpClient in dart?
In a Dart console application, is there a library for an HTTP Request that doesnt require DOM access?
How to do POST in Dart command line HttpClient
This is what I think is true so far but my understanding is fuzzy:
http is high level and HttpClient is low level (source)
http can make post requests but HttpClient can't (source)
both http and HttpClient (with HttpClientRequest) can make GET and POST requests (source)
both http and HttpClient can be used on the client and the server
So to sum it up, I would say that each one can do anything that the other can as well, but it is easier to use the http package since this one is more high-level. Is that summary correct?

The http package is an abstraction over dart:io and dart:html.
So if you want to share code between browser and other platforms that makes HTTP requests, then it's best to use the http package. Then the code will just work everywhere.
If you don't care about the browser use what API you like best. In Flutter the http package just wraps dart:io's HttpClient.

Related

Is it possible for a Prosody server to make an HTTP request to an external API

The Prosody server has several modules that allow it to respond to HTTP requests.
For example, mod_http_rest allows me to make a POST request on port 5280 with an XMPP stanza as the payload, and the Prosody server responds by sending that stanza on its way.
However, I am looking for a module that would do the opposite: i.e. take an XMPP message received by the usual means, and make an HTTP POST request to a specified server with that message as the payload.
I can't seem to find any module that will do this. Is there such a thing?
If not, is there any functionality available in the Prosody API that would allow such a module to be written?
Alternatively, are there good reasons why this might be a bad idea?
To answer your question, yes, it's possible for Prosody to make HTTP requests to external services.
The module you describe (take XMPP and forward it over HTTP) did not exist, but there's little reason for it not to. So I just published a module that provides this feature: https://modules.prosody.im/mod_component_http.html
I dont known if there is a module already for that but there is a way to achieve it yourself. I made my own module some months back for specific purposes and I used net.http which as the Docs say:
Is an API for making non-blocking HTTP requests to remote servers.
So you should check the Docs from here so you can use it like:
local req = require "net.http"
req.request( URL, opts, callBack )
Where opts are your options in json, and callBack is a function that defines what you want to do with the response.
I hope it helps.

In what use cases is a web service likely to receive OPTIONS requests other than CORS?

I have recently implemented a CORS IDispatchMessageInspector applied through a BehaviorExtensionElement for services within a large project I am working on to allow for CORS support (arising from calling REST WCF web services from jQuery Ajax calls).
The current implementation intercepts all OPTIONS method calls to an endpoint with the CORS behavior specified and responds with the appropriate headers (and a 200). As it stands the service will expect to see OPTIONS requests only in the case of CORS requests, however I cannot guarantee that this will always be the case.
In the interests of future proofing and extensibility, what are the most common reasons for OPTIONS requests outside of CORS? Are there plans to extend the use of such requests in future WC3 specs (as this seems to suggest)? Are there any use cases that I should attempt to allow for?
It's the other way around.
A CORS preflight request will be an OPTIONS request including an Origin and Access-Control-Request-Method request header, by which you can recognize it as such.
Any other OPTIONS request is just that, and can be sent by any client for any reason.
WebDAV clients are known to use OPTIONS to probe for support for protocol levels and method support (see RFC 4918).

How to compose a HTTP request with a custom HTTP method type

Is there a tool which enables me to prepare a custom HTTP request. I need to request ressources which are bound to custom HTTP methods on server side.
I know about the fire fox plugin RESTClient which is pretty perfect, except that I can't set a custom HTTP method type like: FOO.
EDIT
I found out that the RESTClient plugin also provides the possibility to create custom HTTP methods. It's the same as with Fiddler. But however, Fiddler is a nice alternative.
I would suggest something similar to fiddler
Postman supports custom HTTP methods...
Source: https://github.com/postmanlabs/postman-app-support/issues/166

Dart's HttpServer and client-side caching

So, I've noticed that using Dart's built in HttpServer class tends to make the client request for every file every time.
On Apache, it is possible to tell the client to cache the file for a maximum of a certain length of time -- does Dart support this feature to lighten the load on HttpSever?
Thanks for the question! You can set any HTTP header in an HTTP response.
For instance:
onRequest(HttpRequest request, HttpResponse response) {
...
response.headers.add("Cache-Control", "max-age=3600");
...
}
If you want more sophisticated handling, such as respecting Etags or If-Modified-Since, you'll probably have to add them yourself. In general, it makes sense to proxy the Dart HTTP Server behind a server such as Nginx or Apache, and then have that server take care serving all of your static files.

Generating HTTP Request

In how many ways can an HTTP request be generated?
There are endless ways how you can create and from where you can send HTTP requests to a server. Actually your server has no idea, what the origin of such a request is (if it's AJAX or "regular" request, or sent from a console application or ...)
But there are HTTP methods (HTTP verbs) that (can) tell the server about the intent of the request: http://en.wikipedia.org/wiki/HTTP_Verbs#Request_methods
Also you can set headers in a request, for example the content-type or the accepted encoding: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
Most JavaScript libraries for example set the (non-standard) HTTP header X-Requested-With, so your application can differentiate between regular and ajax requests.
You see, it's even possible to set your own, non-standard headers. There are endless possible combinations...
HttpRequest is a C# class that wraps a petition sent by a client during a Web request.
There are many ways to generate it. The most usual one happens when your browser connects to an ASP.NET website.
You can, for example, create your own custom HttpRequest to petition a specific web page from a C# console application.
Are you trying to achieve something more specific?

Resources