How to write the post request in wso2 api manager? - wso2

i want to send the POST request with JSON as POST data and it should also accept attachments. I checked the documentation but unable to create the POST

Create a new api with http post type and Payload Parameter name and body parameter type.
if you use form/multipart you must edit some axis2 config file in api manager.
go to wso2am/repository/conf/axis2 and edit axis2.xml.
add this lines:
<messageFormatter contentType="multipart/form-data"
class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>
<messageFormatter contentType="multipart/form-data"
class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>

Related

WSO2 contentType text/html overlap with application/xml in axis2.xml

I have two APIs. Someone returning to contentType text/html. Other one returning contentType application/xml.
I added this parameter in WSO2/ axis2.xml file for wso2 should read to text/html
And i got succes from API but now other API ( i mean return to application/xml) , thats not working correct.
Can someone help me please? How should i do?
Thanks
If you want to enable message relay, so that messages of a specific content type are not built or formatted but simply pass through the ESB, you can specify the message relay builder (org.wso2.carbon.relay.BinaryRelayBuilder) for that content type. Make sure that you specify message builder and formatter for both the content types (application/xml , text/html). Refer to the following sample configuration. You need to add these in the axis2.xml file under messageFormatters and messageBuilders.
Message formatters
<messageFormatter contentType="application/xml"
class="org.apache.axis2.transport.http.ApplicationXMLFormatter"/>
<messageFormatter contentType="text/html"
class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>
Message builders
<messageBuilder contentType="text/xml"
class="org.wso2.carbon.relay.BinaryRelayBuilder"/>
<messageBuilder contentType="application/xml"
class="org.apache.axis2.builder.ApplicationXMLBuilder"/>
[1]-https://docs.wso2.com/display/EI611/Configuring+Message+Relay

Passing #FormParam values to a rest service without using an HTML form

We have an existing rest web service that does a certain online transaction. It was created to receive input of #FormParam type. When we call this web service, we initially just passed the values by appending it to the url
e.g.
/sometransaction?creditCardNumber=123
Problem is, since the number is appended to the url, this gets logged in the web server http requests logs. This cant be since this is sensitive information. We need to pass this the same way a HTML form does a POST submit, it order for the parameters not to be appended to the url and get logged by the web server. Problem is, we don't have a UI page to do this. This is just basically a web service calling another web service.
How can we achieve this?
Code:
#POST
#Path("/dotransaction")
Public Response doTransaction(#BeanParam TxnParams) {
}
Its a rest web service the the params class TxnParams have #FormParam attributes
Ensure the Content-Type is set to application/x-www-form-urlencoded and send the data in the request payload.
Use & to separate the parameters and use = to associate the parameter with its value.
That's what the request will be like:
POST /sometransaction HTTP/1.1
Host: example.org
Content-Type: application/x-www-form-urlencoded
creditCardNumber=4111111111111111&expirationDate=09-2016
And always use HTTPS when sending sensitive information over the wire.

WSO2 API Manager Consume Error While Caching is enabled

When I enable Caching for an API and try to consume it, I receive the response code error 500 with a description - "Error while building message". The response works fine when Caching is turned off. Any ideas?
This error can be occurred if you are passing a empty value to the request body for a POST request. Because the default JSON builder trying to convert JSON to XML and you can see this error when converting an empty message.
If you are sending a empty body for a post request change the builder and formatter to JsonStreamBuilder and JsonStreamFormatter using following steps.
1) Open axis2.xml which is in (APIM_HOME)/repository/conf/axis2 folder and comment the default JSON builder and formatter.
<!--messageBuilder contentType="application/json"
class="org.apache.synapse.commons.json.JsonBuilder"/-->
<!--messageFormatter contentType="application/json"
class="org.apache.synapse.commons.json.JsonFormatter"/-->
2) Un-comment the JsonStreamBuilder and JsonStreamFormatter.
<messageBuilder contentType="application/json"
class="org.apache.synapse.commons.json.JsonStreamBuilder"/>
<messageFormatter contentType="application/json"
class="org.apache.synapse.commons.json.JsonStreamFormatter"/>

Spring Security

I am trying to use spring security in my application developing restful web services and not getting the way how to send request to j_spring_security_check directly so that i can provide same url as a web service to Authorization of username and password.
Currently i am using following request pattern:
URL: "http://localhost:8080/CabFMS/j_spring_security_check"
Content-type: application/x-www-form-urlencoded
Body: {'j_username':'user','j_password':'123'}
Response: is Bad credentials
I am not sure about Content-type and body parameters. Please provide suggestion. To send request i am using REST Client
Tarun Gupta
As you correctly noticed j_spring_security_checks expects application/x-www-form-urlencoded content, therefore you need to encode it as such:
j_username=user&j_password=123
Your request pattern should be :
/j_spring_security_check?j_username=user&j_password=123
with method "POST" type.
other then this you can change request action & parameters text "j_spring_security_check" and "j_username" ,"j_password" by configuring alternate text in attributes :
login-processing-url,
username-parameter,
password-parameter
of spring "form-login" tag in security configuration xml file.
Create an AuthenticationSuccessHadler for this, in the authenticationSuccess method, return base64 encoded authorization header with the pattern username:password
Then, on each request, set the header as Basic yourtokenhere

Problem with Posting JSON object with WSRequest

I want Play to call a webservice. The webservice accepts application/json and returns this as well. With the following code I'm trying to achieve this. (Note, the headers.put(xxx) are added later in an effort to solve the problem).
WSRequest request = WS.url(targetURL);
request.body = new Gson().toJson(user);
request.headers.put("Content-type","application/json");
request.headers.put("Accept","application/json");
request.post();
The strange thing is that my JBOSS server replies: "Cannot consume content type". If I use my 'Simple REST client' plugin in my Chrome browser, and provide the entire JSON Body GSon created and add the content-type header, I get a valid response. Is this not the way to send JSON to the server? Or am I missing some fundamental piece here?
While checking the API documentation on the WSRequest class i noticed the field mime-type
By setting it as follows JBOSS (resteasy) accepted my request succesfully.
request.mimeType = "application/json";