How to increase the Time Out of Rest-Assured API call - rest-assured

Issue is I make the call and The error come back with 'Connection Timed Out'.  how to increase the time out?
 
Here is my code:
RequestSpecification request = RestAssured.given();
Response response = request.get("https://unicef-registry.sdmxcloud.org/ws/public/sdmxapi/rest/data/UNICEF,CME_DF,1.0/.TMM0..269.?format=sdmx-json&includeHistory=true&includeMetadata=true&dimensionAtObservation=AllDimensions&includeAllAnnotations=true");
 

you can use the below code.
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000)
.setSocketTimeout(5000).build();
HttpClientConfig httpClientFactory = HttpClientConfig.httpClientConfig()
.httpClientFactory(() -> HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build());
RestAssured.config = RestAssured.config().httpClient(httpClientFactory);

Related

Checking response code of all URLs in a column [Airtable database]

We have an airtable database of over 24000 records. These records are websites, and many now have errors in them (missing "/", extra space...). We are trying to detect the websites that have these errors before manually fixing them.
What we have tried
So far, we have used the fetch method to call each URL and report back on the error status . This is the script we have used:
const inputConfig = input.config();
const url = inputConfig.url;
let status;
try {
const response = await fetch(url);
status = response.status; } catch (error) {
status = 'error'; }
output.set('status', status);
Issues we ran into
The script won't follow redirects, so it reports "error" back if there is a redirect even if the URL is working.
The output now is either "200" meaning the URL works, or "error". We don't get the actual response code of the error, which we ideally would like to get.
Any help would be appreciated! Thanks
There's some nuance to how fetch works. If you review Mozilla's documentation they say:
The Promise returned from fetch() won't reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, as soon as the server responds with headers, the Promise will resolve normally (with the ok property of the response set to false if the response isn't in the range 200–299), and it will only reject on network failure or if anything prevented the request from completing.
So you have to do an extra check in your code to determine if the request was successful or not and throw your own error. In your case, you don't necessarily need to throw an error at all and can just rely on ok property of the response.
const config = input.config();
const url = config.url;
let status = null;
const response = await fetch(url);
if(response.ok) {
status = response.status
} else {
status = 'error'
}
output.set('status', status);

PostAsJsonAsync does not return response and eventually times out

I have an MVC website that makes an API call using a HttpClient. I configured the timeout to be 2 minutes and the API call generally returns within 90 seconds. I can see from application logging that the API completes its execution. If I also call the API method using a tool like Postman or SOAPUI I get a JSON response after 90 seconds.
I cannot get my MVC application to return the response and instead after 2 minutes it will throw an OperationCanceledException which I catch.
public async Task<CommonApiResult> PostAsync<T>(string url, TimeSpan timeout, T payload)
{
using (var client = new HttpClient())
{
client.Timeout = timeout;
var response = await client.PostAsJsonAsync(url, payload);
if (response.IsSuccessStatusCode)
{
var stringContent = await response.Content.ReadAsString(url).ConfigureAwait(false);
return new CommonApiResult<T>
{
StatusCode = response.StatusCode,
Data = stringContent.Deserialize<T>(url)
};
}
}
return null;
}
I never get to the code if (response.IsSuccessStatusCode).
Is there something obvious I am doing wrong? I have looked at a lot of questions and they all seem to be incorrectly using. Result and not using await like me.
just in case someone visit this link in the future.
if it is called via like button click event please check does the button in form tag and has type submit. if yes remove it from the tag

RestSharp send proper value with header

Please check the RestSharp code bellow. I am passing AddHeader correctly however i am getting response saying i am not passing Content-Type header correctly. How can i fix this issue any idea? Note: api documentation details here. Also checkout debug response screen shot bellow
static void Test()
{
var client = new RestClient("https://api.getresponse.com/v3");
var request = new RestRequest();
request.Method = Method.POST;
request.AddHeader("Content-Type","application/json");
request.AddHeader("X-Auth-Token:", " api-key my-key-here");
request.Parameters.Clear();
var response = client.Execute(request);
}

HTTP response code: 411 for LinkedIn with ApacheOltu

Looking at the https://developer.linkedin.com/docs/oauth2 I try here to get an access token.
What can be wrong with this piece of code ?
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
TokenRequestBuilder r = OAuthClientRequest
.tokenLocation("https://www.linkedin.com/oauth/v2/accessToken")
.setCode(code)
.setGrantType(GrantType.AUTHORIZATION_CODE)
.setClientId(LinkedInClientID)
.setClientSecret(LinkedInClientSecret)
.setRedirectURI("http://localhost:8080/authenticatedLinkedIn");
Map<String,String> m = new HashMap<String,String>();
m.put("Content-Type", "application/x-www-form-urlencoded");
int bodyLength = r.buildBodyMessage().getBody().length();
System.out.println("Body l = " + bodyLength);
m.put("Content-Length", Integer.toString(bodyLength));
r.buildHeaderMessage().setHeaders(m);
OAuthClientRequest request = r.buildQueryMessage();
OAuthJSONAccessTokenResponse tk = oAuthClient.accessToken(request, OAuth.HttpMethod.POST);
I get the following error :
Server returned HTTP response code: 411 for URL:
https://www.linkedin.com/oauth/v2/accessToken?code=AQSZfSXpQ6z3575474fhfbZmxJQofGiwtpw53Y1FnlALvKBWJgQKfJH8kvHM-3f5ZtOqndit594S2cmZrFuiNaXcBOHuSf8yMgFgr4uh-a40Ag&grant_type=authorization_code&client_secret=gHPiGTTyb1KKHPEP&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2FauthenticatedLinkedIn&client_id=86txkd469mat
You need to send a POST request with params in body urlenconded... so
First change:
OAuthClientRequest request = r.buildQueryMessage();
With:
OAuthClientRequest request = r.buildBodyMessage();
Then you do not need to put any header Oltu will manage this for U.
Hope it will help.

PowerBuilder Vertex connection

I am trying to connect to a Vertex tax database using PowerBuilder 11.1 and am having problems with the following code.
I think I am connecting correctly since the return codes for ls_status_text = loo_xmlhttp.StatusText is 200 and ll_status_code = loo_xmlhttp.Status is OK.
When I get the return value from the ls_response_text = loo_xmlhttp.ResponseText code the return value is the MOTW message.
I am expecting the following code to send the ls_get_url (which contains the xml to be sent to vertex) and receive a large xml in return with calculated tax rates based off the ls_get_url xml. What I am getting is ls_status_text = 'OK' and ll_Status_code = 200 ( anything >300 is a problem).
// Get request
loo_xmlhttp.open ("GET",ls_get_url , false)
loo_xmlhttp.send()
//Get response
ls_status_text = ''
ls_status_text = loo_xmlhttp.StatusText
ll_status_code = loo_xmlhttp.Status
after the above block of code runs successfully the following code runs:
if ll_status_code >= 300 then
MessageBox("HTTP POST Request Failed", ls_response_text)
else
//Get the response we received from the web server
ls_response_text = loo_xmlhttp.ResponseText
MessageBox("POST Request Succeeded", ls_response_text)
end if
I get the "POST Request Succeeded" messagebox but the ls_response_text contains the Mark Of The Web syntax.
Do you have any ideas that can help me along?
Thanks!
String ls_get_url, ls_post_url
String ls_post_variables, ls_response
String ls_response_text, ls_status_text
long ll_status_code
OleObject loo_xmlhttp
//include parameters on the URL here for get parameters
ls_get_url = 'http://10.1.1.65:8095/vertex-ui/vertextcc.jsp'
try
//Create an instance of our COM object
loo_xmlhttp = CREATE oleobject
loo_xmlhttp.ConnectToNewObject( 'Microsoft.XMLHTTP')
// Get request
loo_xmlhttp.open ("GET",ls_get_url , false)
loo_xmlhttp.send()
//Get response
ls_status_text = ''
ls_status_text = loo_xmlhttp.StatusText
ll_status_code = loo_xmlhttp.Status
//Check HTTP Response code for errors
if ll_status_code >= 300 then
MessageBox("HTTP GET Request Failed", ls_response_text)
else
//Get the response we received from the web server
ls_response_text = loo_xmlhttp.ResponseText
MessageBox("GET Request Succeeded", ls_response_text)
end if
ls_post_url = 'http://10.1.1.65:8095/vertex-ui/vertextcc.jsp'
ls_post_variables = "I add my custom xml here - I can run it in the vertex software and the xml executes fine"
loo_xmlhttp.open ("POST",ls_post_url, false)
loo_xmlhttp.send(ls_post_variables)
//Get response
ls_status_text = loo_xmlhttp.StatusText
ll_status_code = loo_xmlhttp.Status
//Check HTTP Response code for errors
if ll_status_code >= 300 then
MessageBox("HTTP POST Request Failed", ls_response_text)
else
//Get the response we received from the web server
ls_response_text = loo_xmlhttp.ResponseText
MessageBox("POST Request Succeeded", ls_response_text)
end if
loo_xmlhttp.DisconnectObject()
catch (RuntimeError rte)
MessageBox("Error", "RuntimeError - " + rte.getMessage())
end try
There's an On Demand Vertex service. I hit the .../vertex-ui/vertextcc.jsp address on port 80 and got a login prompt. So it appears you need to Post the login data before you start shoving XML at it. I couldn't look any farther as I don't have an account. I don't know what the server would give you after you log in, but if it's a page you can paste XML into, you could install Fiddler and see exactly what belongs in the Post. Fiddler will also show you what Microsoft XMLHTTP is posting and what the server is sending back.

Resources