How does one POST JSON encoded content in tsung? - tsung

Here's what I have:
<session name="es_load" probability="100" type="ts_http">
<request subst="true">
<http url='/access.log' version='1.0'
contents='{"username"="rsss","password"="pass"}'
content_type='application/json' method='POST'>
</http>
</request>
</session>
I don't see this even getting posted on the backend.

Switched to locust.io and couldn't be happier!

<request>
<http url="/portal/v2/portal" method="POST" version="1.1" contents='{
"status": "online",
"language": "en",
"preference": {
"optIn": "1"
}
}'>
<http_header name="authorization" value="Bearer token"/>
</http>
</request>

Instead of 'contents' attribute use the "contents_from_file" attribute just like below,
<request subst="true">
<http url="https://**API**" method="POST"
**contents_from_file**="file.json">
<http_header name="accept" value="*/*"/>
<http_header name="content-type" value="application/json"/>
<http_header name="authorization" value="%%_token%%"/>
</http>
</request>
Note: In this 'file.json' provide the data properly and save it.

Related

how to add mediation for get token of WSO2 API Manager?

I Have This API And I Used the Message Mediation Below to Authenticate and Get the Token:
curl -X POST "URL" -H "accept: /" -H "Content-Type: application/x-www-form-urlencoded" -d "username=user&password=password"
result is:
enter image description here
<payloadFactory media-type="x-www-form-urlencoded">
<format>
{
"username":"user",
"password":"password"
}
</format>
<args>
</args>
</payloadFactory>
<header name="Content-Type" value="application/x-www-form-urlencoded" scope="transport" />
<property name="messageType" value="application/json" scope="axis2" type="STRING" />
<property name="REST_URL_POSTFIX" value="" scope="axis2" type="STRING" />
<call blocking="true">
<endpoint>
<http method="POST" uri-template="URL" />
</endpoint>
</call>
<property name="bearer-token" expression="json-eval($..token)" scope="default" type="STRING" />
<property name="REST_URL_POSTFIX" expression="get-property('resource')" scope="axis2" type="STRING" />
<header name="Authorization" expression="fn:concat('Bearer ', get-property('bearer-token'))" scope="transport" />
<payloadFactory media-type="x-www-form-urlencoded">
<format>$1</format>
<args>
<arg evaluator="xml" expression="get-property('request-body')" />
</args>
</payloadFactory>
But i have this error:
enter image description here
WSO2 API Manager Logs
INFO - LogMediator STATUS = Message dispatched to the main sequence. Invalid URL., RESOURCE = url, HEALTH CHECK URL = url
[2021-07-28 16:22:39,696] INFO - LogMediator STATUS = Message dispatched to the main sequence. Invalid URL., RESOURCE = url, HEALTH CHECK URL = url
Make sure that, you have provided the correct token endpoint url for the Call mediator
<call blocking="true">
<endpoint>
<http method="POST" uri-template="https://your-token-endpoint/oauth" />
</endpoint>
</call>

Call mediator end point type(XPath)

In call mediator we can form endpoint using XPath (from the response).But the problem is endpoint is not calling.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/xpath" name="call"
xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" protocol="http">
<inSequence>
<call>
<endpoint key-expression="/root/a"/>
</call>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
following response is from postman.
<root>
<a>http://www.mocky.io/v2/5ca6db71340000132f76b192</a>
</root>
Expected response :
<root>
<name>abcd</name>
<no>82382832</no>
</root>
Firstly, when using the resolving endpoint (using key-expression), we cannot give the URL directly [1]. We have to have the endpoint defined beforehand and should give only the key of the endpoint in the payload.
Secondly, for the key-expression to parse the xpath, the message should be built beforehand. Since call mediator is content-unaware, it will not build the message. Therefore, we should use a content aware mediator to build the message.
Following is the sample inSequence that'd work.
<inSequence>
<log level="full"/>
<call>
<endpoint key-expression="//a"/>
</call>
<respond/>
</inSequence>
Now the payload should be like
<root>
<a>testEndpoint</a>
</root>
Edit:
The endpoint named 'testEndpoint' should be defined with the backend url.
You need to define an endpoint [2]. For example, I'm using an address endpoint.
<endpoint xmlns="http://ws.apache.org/ns/synapse" name="testEndpoint">
<address uri="http://www.mocky.io/v2/5ca6db71340000132f76b192">
<suspendOnFailure>
<progressionFactor>1.0</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
<retryDelay>0</retryDelay>
</markForSuspension>
</address>
</endpoint>
Try this. I receieved needed response in Postman, but you should use POST method as your request has a Body.
<api xmlns="http://ws.apache.org/ns/synapse" name="call" context="/xpath">
<resource methods="POST">
<inSequence>
<property name="uri.var.httpendpointurl" expression="$body/root/a" scope="default" type="STRING"/>
<call>
<endpoint>
<http uri-template="{uri.var.httpendpointurl}"/>
</endpoint>
</call>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>

getting "org.apache.http.nohttpresponseexception the target server failed to respond" while calling wso2 http endpoint

I am getting below error while calling http endpoint:-
The endpoint is HTTP endpoint which I am using.
Also added timeout parameters in axis2.xml and axis2.blocking.xml.
But it did not work.
Can anyone provide any solution to it?
HTTPSender Unable to sendViaPost to url[http://*********]
org.apache.commons.httpclient.NoHttpResponseException: The server ****** failed to respond
at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1976)
at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1735)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1098)
the esb proxy looks like below:--
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="mqListener"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<log description="print message" level="full">
<property name="message" value=""In listenAbc1Mq""/>
</log>
<payloadFactory media-type="json">
<format>
{
"orderId": ""
}
</format>
<args>
<arg evaluator="xml" expression="//verkkopvc/text()"/>
</payloadFactory>
<log description="print message" level="full">
<property name="message" value=""after payload factory""/>
</log>
<property name="OUT_ONLY" value="true"/>
<property name="DISABLE_CHUNKING"
scope="axis2"
type="STRING"
value="true"/>
<call blocking="true">
<endpoint key="abc"/>
</call>
<log description="print message" level="full">
<property name="message" value=""Out sequence""/>
</log>
</inSequence>
<outSequence/>
<description/>
</proxy>
I believe the error is printed by your http client. To fix it, do these.
1) Remove <property name="OUT_ONLY" value="true"/>
2) Add <respond/> mediator at the end of inSequence.

Spring Security 4 - CredentialsExpiredException not redirecting to reset password - XML config

I am using Spring-Security 4 XML configuration to successfully implement password authentication in a spring-mvc webapp.
The problem I have is that when CredentialsExpiredException is thrown by DaoAuthenticationProvider, the system redirects to login-form, instead of reset password.
My context-security xml configuration is as follow:
<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<b:bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<b:constructor-arg value="/index/form" />
</b:bean>
<http auto-config="true" use-expressions="true" disable-url-rewriting="true" entry-point-ref="authenticationEntryPoint" >
<intercept-url pattern="/" access="permitAll" requires-channel="https"/>
<intercept-url pattern="/index" access="permitAll" requires-channel="https"/>
<intercept-url pattern="/index/*" access="permitAll" requires-channel="https"/>
<intercept-url pattern="/**" access="hasAnyRole('USER','SYS_ADMIN' )" requires-channel="https"/>
<form-login
login-page="/index/form"
default-target-url="/dashboard"
login-processing-url="/index"
username-parameter="username"
password-parameter="password"
authentication-failure-handler-ref="exceptionTranslationFilter"
always-use-default-target="true"/>
<logout logout-url="/logout" logout-success-url="/index/logout"/>
<session-management>
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
</session-management>
</http>
<!-- password expiry functionality starts -->
<b:bean id="exceptionTranslationFilter" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
<b:property name="exceptionMappings">
<b:props>
<b:prop key="org.springframework.security.authentication.CredentialsExpiredException">/resetpassword</b:prop>
</b:props>
</b:property>
<b:property name="defaultFailureUrl" value="/index/error"/>
</b:bean>
<b:bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
<b:constructor-arg name="providers">
<b:list>
<b:ref bean="daoAuthenticationProvider"/>
</b:list>
</b:constructor-arg>
</b:bean>
<b:bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider ">
<b:property name="userDetailsService" ref="customUserDetailsService" />
<b:property name="passwordEncoder" ref="passwordEncoder" />
</b:bean>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="customUserDetailsService" />
<authentication-provider ref="daoAuthenticationProvider" />
</authentication-manager>
<b:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" >
</b:bean>
Given the above configuration, when I enter correct username and password of user whose credentials have expired, I get redirected to url = "/index/form"
I have ran the debugger (I'm using Eclipse), and the code execution is as follows (all classes belong to Spring Security 4):
AbstractUserDetailsAuthenticationProvider.authenticate() throws CredentialsExpiredException when executing postAuthenticationChecks.check(user);
ExceptionMappingAuthenticationFailureHandler.onAuthenticationFailure() gets the url to be /resetpassword before calling getRedirectStrategy().sendRedirect(request, response, url);
DefaultRedirectStrategy.sendRedirect() gets the redirect url to be "/myapp/resetpassword"
The problem occurs on LoginUrlAuthenticationEntryPoint.commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException).
Because useForward is set to false, it calls redirectUrl = buildRedirectUrlToLoginPage(request, response, authException);
redirectUrl ends up being "/index/form".
Even if I set useForward to true and subclass LoginUrlAuthenticationEntryPoint in order to override determineUrlToUseForThisRequest the AuthenticationException I get is of type InsufficientAuthenticationException.
The interesting thing is that the url on my browser is https://localhost:8443/myapp/resetpassword, but what it displays is the login form.
Have you encountered this problem before? If so, how did you get spring to redirect to reset password?
Most of the configuration I obtained from https://stackoverflow.com/a/14383194/158499
Thanks in advance, Lucas
<intercept-url pattern="/**" access="hasAnyRole('USER','SYS_ADMIN' )" requires-channel="https"/>
when redirect to /resetpassword will redirect to login page. Please allow this url to access without authentication.
<intercept-url pattern="/resetpassword" access="permitAll" requires-channel="https"/>

Link two entities OData

I'm try getting deep with OData and I've created a service with the following metadata:
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="2.0">
<Schema xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="HelloOdata.library">
<EntityType Name="booksType">
<Key>
<PropertyRef Name="title"/>
</Key>
<Property Name="title" Type="Edm.String" Nullable="false" MaxLength="100"/>
<Property Name="ISBN" Type="Edm.String" MaxLength="100"/>
<Property Name="editions" Type="Edm.Int32"/>
<NavigationProperty Name="Author" Relationship="HelloOdata.library.WroteType" FromRole="booksPrincipal" ToRole="writersDependent"/>
</EntityType>
<EntityType Name="writersType">
<Key>
<PropertyRef Name="name"/>
<PropertyRef Name="birth"/>
</Key>
<Property Name="name" Type="Edm.String" Nullable="false" MaxLength="100"/>
<Property Name="birth" Type="Edm.DateTime" Nullable="false"/>
<NavigationProperty Name="Titles" Relationship="HelloOdata.library.BooksType" FromRole="writersPrincipal" ToRole="booksDependent"/>
</EntityType>
<Association Name="WroteType">
<End Type="HelloOdata.library.booksType" Role="booksPrincipal" Multiplicity="*"/>
<End Type="HelloOdata.library.writersType" Role="writersDependent" Multiplicity="1"/>
</Association>
<Association Name="BooksType">
<End Type="HelloOdata.library.writersType" Role="writersPrincipal" Multiplicity="1"/>
<End Type="HelloOdata.library.booksType" Role="booksDependent" Multiplicity="*"/>
</Association>
<EntityContainer Name="library" m:IsDefaultEntityContainer="true">
<EntitySet Name="books" EntityType="HelloOdata.library.booksType"/>
<EntitySet Name="writers" EntityType="HelloOdata.library.writersType"/>
<AssociationSet Name="Wrote" Association="HelloOdata.library.WroteType">
<End Role="booksPrincipal" EntitySet="books"/>
<End Role="writersDependent" EntitySet="writers"/>
</AssociationSet>
<AssociationSet Name="Books" Association="HelloOdata.library.BooksType">
<End Role="writersPrincipal" EntitySet="writers"/>
<End Role="booksDependent" EntitySet="books"/>
</AssociationSet>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
Do you have any idea how to create a link between a Write and a Book? I've tried to read the examples in the OData documentation but I haven't got success and my tries. I'm using OData data protocol v2.
Thank you.
Pablo
Well I've created this POST request:
POST /HelloOdata/library.xsodata/books('HAPI%20Hana%20Integration%20Test%20Volume%201')/$links/Author HTTP/1.1
Host: xxxxx
DataServiceVersion: 1.0
MaxDataServiceVersion: 2.0
Content-Type: application/xml
<uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
http://host-service/HelloOdata/library.xsodata/writers(name='Robert%20Kingst',birth=datetime'2008-04-08T00:00:00.0000000')
</uri>
and I've got the following response:
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<code />
<message xml:lang="en-US">The URI is not valid for POST operation. The URI must point to an entity set for POST operations.</message>
</error>
Suppose you want to link the author "Charles Dickens" to the book "Oliver Twist", then you send a POST request to your service:
POST http://host/service/booksType('Oliver Twist')/$links/Author
<uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
http://host/service/writersType(name='Charles Dickens',birth='1812-02-07T00:00:00')
</uri>
Since Author is not collection, try changing the method from POST to PUT.

Resources