We have a simple F# console app that sends a HTTP POST request to a WebAPI endpoint via FSharp.Data Http.Request. We are using the customizeHttpRequest parameter in order to try to set the request timeout property. Our usage is as follows:
let response = Http.Request(
serviceEndpoint,
headers = requestHeaders,
body = requestBody,
silentHttpErrors = true,
customizeHttpRequest = (fun request -> request.Timeout <- 1000; request))
We are observing that our custom timeout is ignored (i.e. the request does not timeout after 1 second as in this example). We have also observed that the request will not timeout after the default System.Net.HttpWebRequest timeout of 100,000ms.
Is there an issue here, or are we not using the customizeHttpRequest parameter correctly?
FSharp.Data uses the asynchronous GetResponse method. According to MSDN, in that case the timeout set on the HttpWebRequest object isn't considered and handling the timeout is left for the client code to implement.
Unfortunately FSharp.Data doesn't do it. There's an open issue for implementing it here.
Related
I have an issue with RestClient response is coming back as
"StatusCode: 0, Content-Type: , Content-Length: )"
with the ErrorMessage of
"The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing."
Is this a timeout on the url's end or my httpclient? Is request.timeout correct? It might take 5+ minutes for this request even though it's only 170KB of data due to their end being poorly optimized.
var client = new RestClient(url);
RestRequest request = new RestRequest() { Method = Method.Get };
request.Timeout = 300000;
request.AddParameter("access_token", AccessToken);
request.AddParameter("start_date", StartDate.ToString("yyyy-MM-dd"));
request.AddParameter("end_date", EndDate.ToString("yyyy-MM-dd"));
request.AddParameter("offset", offset.ToString());
var response = await client.ExecuteAsync(request);
var responseWorkLoads = JObject.Parse(response.Content).SelectToken("worklogs");
There are two timeouts that RestSharp allows you to set.
When you create a new instance of RestClient, you can specify the HttpClient timeout that will override the default 100 ms using RestOptions:
var client = new RestClient(new RestClientOptions { Timeout = 300000 });
As the wrapped HttpClient is instantiated and configured once per RestClient instance, setting the request timeout doesn't override that setting, otherwise the client won't be thread-safe.
The request timeout, on the other hand, overrides the client timeout if it is less than the client timeout. RestSharp creates a cancellation token source using the request timeout, so the request will be cancelled when the linked cancellation token cancels.
I believe that currently RestClient also doesn't set the failure reason properly when the client times out, only if the token gets cancelled. I will create an issue for that.
I have a Lua proxy that needs to route requests. Each request destination is established based on the response from another HTTP request with a header from the initial request. My understanding is that HAProxy is an event-driven software, so blocking system calls are absolutely forbidden and my code is blocking because is doing an HTTP request.
I read about yielding after the request but I think it won't help since the HTTP request is already started. The library for doing the request is https://github.com/JakobGreen/lua-requests#simple-requests
local requests = require('requests')
core.register_fetches('http_backend', function(txn)
local dest = txn.sf:req_fhdr('X-dest')
local url = "http://127.0.0.1:8080/service";
local response = requests.get(url.."/"+dest);
local json = response.json()
return json.field
end )
How do I convert my code to be non-blocking?
You should consider using HAProxy's SPOE which was created exactly for these blocking scenarios.
I managed to do it using Lua. The thing I was making wrong was using require('requests') this is blocking. Ideally for HA never use a Lua external library. I have to deal with plain sockets and do an HTTP request and very important to use HA core method core.tcp() instead of Lua sockets.
I'm trying to find a way for executing http requests with OAuth authorization. Basically I already have all required secrets/tokens
For sending such request should be signed in a rather tricky way described here: https://oauth1.wp-api.org/docs/basics/Signing.html
Are there any libraries/examples for this suitable for Akka HTTP client's API?
Since I didn't find any solution for that, I've implemented method for GET-requests:
https://gist.github.com/Blackmorse/cdb5e13d749e7902ad47d5a168dd23ca
Usage:
E.g. you want request data from s"$URL/$API_ENDPOINT?param1=value1¶m2=value2".
To construct the Akks's akka.http.scaladsl.model.HttpRequest object:
val request = create(Map("param1" -> "value1", "param2" -> "value2"))
And use it:
Source.single((request, someData)).via(Http().cachedHostConnectionPoolHttps[T](URL))
HI I have been trying to call REST POST API using jersey REST Client. The API is docs is
URL:
METHOD: POST
Header Info:-
X-GWS-APP-NAME: XYZ
Accept: application/json or application/xml
My Sample Jersey client code is
Client client = Client.create();
WebResource resource=client.resource(URL);
resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML);
resource.type(javax.ws.rs.core.MediaType.APPLICATION_XML);
resource.type("charset=utf-8");
ClientResponse response = resource.post(ClientResponse.class,myReqObj);
I have been trying this code variation since last 1 week and it is not working. Any help in this regard is highly appreciated.
The tricky part is that the WebResource methods follows the Builder design pattern so it returns a Builder object which you need to preserve and carry on as you call further methods to set the full context of the request.
When you do resource.accept, it returns something you don't store, so it's lost when you do resource.type and therefore only your last call takes effect.
You'd typically set all the criterias in one line, but you could also save the output in a local variable.
ClientResponse response = client.resource(URL)
.accept(MediaType.APPLICATION_XML)
.type(MediaType.APPLICATION_XML)
.post(ClientResponse.class,myReqObj);
I do like that.
Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE)
.accept(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(a, "application/json; charset=UTF-8"));
here, 'a' is account class instance which like
#XmlRootElement
public class account {
...
...
}
I'm building a firefox add-on using the add-on sdk. I need to make a http request to a certain page and I want to handle the connection timeout but couldn't find anything in the api: https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/request.html
What I'm actually looking is a callback in case the client couldn't connect to the server.
Is there a way to achieve this?
The SDK request will always call onComplete, when the request is considered done for the network. This means that onComplete is called in any case, disregarding if the request returned an error or a success.
In order to detect which error you've got, you need to check the Response object's (the object passed to the onComplete function) property "status" (response.status). It holds the status code for the request. To look up status codes, consider the list on the mozilla developer network. If the response status is 0, the request has failed completely and the user is probably offline, or the target couldn't be reached.
A timeout would either be a status code 504 or 0. The implementation would be similar to this:
var Request = require("sdk/request");
Request({
url: "http://foo.bar/request.target",
onComplete: function(response) {
if(response.status==0||response.status==504) {
// do connection timeout handling
}
// probably check for other status codes
else {
// assume the request went well
}
}
}).get();
I personally use a validation function on the request object, which returns me a number which depends whether I've got a correct response, an error from the web server or a connection issue (4xx and 0 status codes).