HTTP OPTIONS in Xamarin Forms - rest

Getting "204 Status Code" as No Content

Here is a simple example of OPTIONS request:
var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Options, new Uri("http://myapi.com"));
var response = await httpClient.SendAsync(request);
Just wondering why should you need to make one? OPTIONS is used to identify allowed request methods:
To find out which request methods a server supports, one can use curl
and issue an OPTIONS request:
curl -X OPTIONS http://example.org -i
The response then contains an Allow header with the allowed methods:
HTTP/1.1 200 OK
Allow: OPTIONS, GET, HEAD, POST
Cache-Control: max-age=604800
Date: Thu, 13 Oct 2016 11:45:00 GMT
Expires: Thu, 20 Oct 2016 11:45:00 GMT
Server: EOS (lax004/2813)
x-ec-custom-error: 1
Content-Length: 0
Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS

Related

Downloading data from REST API response but getting a response code of -1

REST API details from documentation:
GET /REST/sql_snapshot/2003-03-01.sql.gz HTTP/1.1
Host: some.api.net
Authorization: Basic qpow3i12o3
The response shown below omits the message body, which contains binary compressed SQL data.
HTTP/1.1 200 OK
Date: Wed, 05 Mar 2003 10:19:46 GMT
Server: Apache/1.3.22 (Unix) (Red-Hat/Linux)Content-Type: application/octet-stream
I'm getting a response code of -1 from below.
URL url = new URL("https://some.api.net/REST/sql_snapshot/2003-03-01.sql.gz");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/octet-stream");
connection.setDoOutput(true);
connection.connect();

How to get last-modified from http header with dartio HttpClient

Hi there I want get Last_modified from http header with dart.io HttpClient()
code sample is:
var client = new HttpClient();
HttpClientRequest req = await client.getUrl(Uri.parse("sayagh.asnafhormozgan.ir/wp-content/tables/essentials.csv"));
var a = req.headers.value("lastModifiedHeader");
but a returns null;
how I can get Last modified?
but when I get it with curl:
curl -v "sayagh.asnafhormozgan.ir/wp-content/tables/drawer.csv"
* Trying 51.89.173.235:80...
* TCP_NODELAY set
* Connected to sayagh.asnafhormozgan.ir (51.89.173.235) port 80 (#0)
> GET /wp-content/tables/drawer.csv HTTP/1.1
> Host: sayagh.asnafhormozgan.ir
> User-Agent: curl/7.65.3
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx
< Date: Thu, 19 Sep 2019 10:05:37 GMT
< Content-Type: text/csv
< Content-Length: 599
< Connection: keep-alive
< Last-Modified: Thu, 19 Sep 2019 09:38:30 GMT
< Accept-Ranges: bytes
< Cache-Control: max-age=0
< Expires: Thu, 19 Sep 2019 10:05:37 GMT
<
You'll need to wait for the second future to get the response, i.e.:
A getUrl request is a two-step process, triggered by two Futures. When
the first future completes with a HttpClientRequest, the underlying
network connection has been established, but no data has been sent. In
the callback function for the first future, the HTTP headers and body
can be set on the request. Either the first write to the request
object or a call to close sends the request to the server.
See https://api.dartlang.org/stable/2.5.0/dart-io/HttpClient-class.html
Also it should be 'last-modified' and not 'lastModifiedHeader' (or even better use the static const variable HttpHeaders.lastModifiedHeader), e.g.:
HttpClient client = HttpClient();
HttpClientRequest req = await client.getUrl(Uri.parse(
'http://sayagh.asnafhormozgan.ir/wp-content/tables/essentials.csv'));
HttpClientResponse response = await req.close();
print(response.headers.value(HttpHeaders.lastModifiedHeader));

How do I make the default response Content-type: application/json with Scalatra

I'm learning Scalatra and am wondering how I can make the default Content-Type of my responses application/json. The current default appears to be text/html. Neat, but not really useful to my application.
The current default is text/html.
$ curl -i -X GET 'http://localhost:8080/v1/example'
HTTP/1.1 200 OK
Date: Fri, 19 Apr 2019 07:21:21 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 23
Server: Jetty(9.4.8.v20171121)
HelloWorld(hello,world)
I can get application-json explicitly through the Accepted: application/json header.
$ curl -i -X GET 'http://localhost:8080/v1/example' -H 'Accept: application/json'
HTTP/1.1 200 OK
Date: Fri, 19 Apr 2019 07:22:09 GMT
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Server: Jetty(9.4.8.v20171121)
{"hello":"hello","world":"world"}
How do I set the default to be application/json.
defaultFormat could be overridden to make application/json the default case. For example,
import java.io.File
import org.scalatra._
import org.scalatra.util.MimeTypes
import org.json4s.{DefaultFormats, Formats}
import org.scalatra.json._
case class HelloWorld(hello: String, world: String)
class MyScalatraServlet extends ScalatraServlet with JacksonJsonSupport {
protected implicit lazy val jsonFormats: Formats = DefaultFormats
override def defaultFormat: Symbol = 'json
get("/v1/example") {
HelloWorld("hello", "world")
}
}
without specifying Accept header should respond with
curl -i -X GET 'http://localhost:8080/v1/example'
HTTP/1.1 200 OK
Date: Thu, 25 Apr 2019 22:28:37 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 33
Server: Jetty(9.4.8.v20171121)
{"hello":"hello","world":"world"}
whilst explicitly requesting Accept: text/html also works
curl -i -X GET 'http://localhost:8080/v1/example' -H 'Accept: text/html'
HTTP/1.1 200 OK
Date: Fri, 26 Apr 2019 15:45:22 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 23
Server: Jetty(9.4.8.v20171121)
HelloWorld(hello,world)

Having Get single and Get all methods on a ApiController

I'm developing an API using close to the latest bits from the aspnetwebstack Codeplex project (4592e2f63c55 from 2012-05-09 if anyone is interested).
I have the following route:
context.Routes.MapHttpRoute("SiteSpecific", "Api/{controller}/{customerId}/{siteToken}/{id}",
new { id = UrlParameter.Optional });
And what I'm currently trying to do is implement get single and a get all in an ApiController. The Get all method, for testing is the following:
public IEnumerable<EditChatResponse> Get(string customerId, string siteToken)
{
return new []{new EditChatResponse{Template = "Get All"}, };
}
And the get single is currently following:
public EditChatResponse Get(string customerId, string siteToken, string id)
{
return new EditChatResponse {Template = "Get Single"};
}
However, routing is always choosing the Get single method:
$ curl -i -H "Accept: applicaiton/json" http://localhost/api/chatresponse/a/b
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 14 May 2012 18:06:26 GMT
Content-Length: 66
{"Id":0,"Template":"Get Single","Inherited":false,"Enabled":false}
$ curl -i -H "Accept: applicaiton/json" http://localhost/api/chatresponse/a/b/c
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 14 May 2012 18:06:28 GMT
Content-Length: 66
{"Id":0,"Template":"Get Single","Inherited":false,"Enabled":false}
I've tried renaming the Get all method to GetAll, as I've seen in some examples, decorating it with [HttpGet], but it still chooses the single method.
Am I completely missing something, or do I need to go about this a different way (most of the examples I see look to be related to the beta bits, and not a recent version from CodePlex)?
Try using this for the default id parameter:
new { id = System.Web.Http.RouteParameter.Optional }

How can I get both the GET and POST request params, on a POST request?

I'm creating a facebook app with a Perl backend. The problem is that since Facebook sends the request to my web app as a POST request I'm having a problem getting the GET parameters that were also part of the base URL for the application -- in effect I'm only getting the POST params from $CGI->Vars.
See CGI/MIXING POST AND URL PARAMETERS.
Short version: use $CGI->param() for post paramenters and $CGI->url_param() for query string parameters.
Dump CGI in favour of a better interface. Plack's param method returns GET and POST parameters mixed.
plackup -MPlack::Request -e 'sub {
my ($env) = #_;
my $r = Plack::Request->new($env);
return [200, ["Content-Type" => "text/plain"], [join "\n", $r->param("foo")]];
}'
> lwp-request -m POST -USe 'http://localhost:5000/fnord?foo=bar;baz=quux'
Please enter content (application/x-www-form-urlencoded) to be POSTed:
foo=123;baz=456
␄
POST http://localhost:5000/fnord?foo=bar;baz=quux
User-Agent: lwp-request/6.03 libwww-perl/6.03
Content-Length: 16
Content-Type: application/x-www-form-urlencoded
200 OK
Date: Thu, 27 Oct 2011 21:27:46 GMT
Server: HTTP::Server::PSGI
Content-Length: 7
Content-Type: text/plain
Client-Date: Thu, 27 Oct 2011 21:27:46 GMT
Client-Peer: 127.0.0.1:5000
Client-Response-Num: 1
bar
123
Just set $CGI::APPEND_QUERY_STRING = 1;