Create parameters in API Header in WSO2 - wso2

I have created a API in WSO2 studio integrator like below:
curl --location --request POST 'http://localhost:8290/internal/send-messages'
--header 'accept: text/plain'
--header 'Content-Type: application/json'
--data-raw '{
"bankName": "heere",
"uniqueIdentifier": "445334564"
}'
To pass the requests (which received from this API) toward the endpoint, I need to add two following parameters into API Header
--header 'MasterName: TOMSON'
--header 'clientName: TOM' \
TOMSON value is unique(static) and would NOT change for any user.
However TOM would be change based on application username given from API Manager (there is OATH2 authentication).
How can I add a static header (Like MasterName) to a API while sending toward the endpoint
How can I understand the username of application ??

You can add a static header using the header mediator[1].
<header name="MasterName" value="TOMSON" scope="transport"/>
To get the application information from the API Manager, you can enable backend JWT[2]. This will be a JWT generated with much more information regarding the request. By decoding this[3] JWT at the backend, you can get the application information with the claim http://wso2.org/claims/applicationname.
[1] - https://ei.docs.wso2.com/en/latest/micro-integrator/references/mediators/header-Mediator/
[2] - https://apim.docs.wso2.com/en/latest/deploy-and-publish/deploy-on-gateway/api-gateway/passing-enduser-attributes-to-the-backend-via-api-gateway/
[3] - WSO2 Decode JWT

You can use header mediator to add headers
For example
<header name="miCustomHeader" value="ThisIsTheWay" scope="transport"/>
<send>
<endpoint>
<address uri="https://someRandomURL"/>
</endpoint>
</send>
It will send miCustomHeader along its value to the endpoint

Related

WSO2 EI 6.6 Content-Type is null for GET requests

I'm having an API endpoint defined in WSO2 EI Integration studio. When I send this endpoint a GET request, the $axis2:ContentType header of the request is getting null as soon as it comes inside the EI (Beginning of inSequence) . Is there a way to preserve this header and send it out of the EI. Any idea how to do this ?
NOTE: This is not happening for POST requests.
API endpoint
<?xml version="1.0" encoding="UTF-8"?>
<api context="/eirouter_test" name="eirouter_test" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST GET">
<inSequence>
<log level="custom">
<property expression="$axis2:ContentType" name="original-content-type" />
</log>
<respond description="Send result to the client"/>
</inSequence>
<outSequence />
<faultSequence />
</resource>
</api>
Request CURL
curl --location --request GET 'http://localhost:8290/eirouter_test/eb805bbe-6c3e-414d-a380-857b6f89607c' \
--header 'Content-Type: application/json'
EI Logs
[2021-06-01 10:54:41,457] INFO {LogMediator} - {api:eirouter_test} original-content-type = null
In short answer, the Content-Type HTTP header should be set only for PUT and POST requests. There was very similar question: Do I need a content-type header for HTTP GET requests?
with very good explanations.
So that is not WSO2 problem, but that is how works HTTP with Content-Type header.

Passing a custom authorization token to the backend in WSO2 is not giving the expected result

I am using WSO2 APIM-2.6.0 and I would like to pass a custom token. For which I have used a token exchange mechanism in mediation Policies, In flow. As per the instructions provided on WSO2 documentation from the below link,
https://apim.docs.wso2.com/en/latest/Learn/APIGateway/MessageMediation/passing-a-custom-authorization-token-to-the-backend/
Token exchange xml used,
<sequence xmlns="http://ws.apache.org/ns/synapse" name="TokenExchange">
<property name="Custom" expression="get-property('transport', 'Custom')"/>
<property name="Authorization" expression="get-property('Custom')" scope="transport"/>
<property name="Custom" scope="transport" action="remove"/>
</sequence>
On the management console of API Store when I provide the existing bearer token in the header parameter and when I execute, the response is,
TypeError: Failed to fetch
Code: 200
But, when I copy the same curl command from API Store UI and run through command prompt, it gives the response and it works fine. How can I fix this?

WSO2EI API list

I need to get all apis(list of apis) with their parameters from wso2ei. I could do that with wso2 api-manager with curl command.how can I do this with curl command in wso2ei?
There aren't any rest APIs available in wso2ei to get the list of APIs.
You can retrieve API list and details from RestApiAdmin admin service (SOAP service). You can get parameters and types from the WSDL. Follow [1] to retrieve WSDL of RestApiAdmin Admin service.
Sample curl to retrieve API list:
curl -k -v -X POST \
https://localhost:9443/services/RestApiAdmin \
-H 'Accept: */*' \
-H 'Authorization: Basic YWRtaW46YWRtaW4=' \
-H 'SOAPAction: "urn:getAPIsForListing"' \
-d '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://org.apache.axis2/xsd">
<soapenv:Header/>
<soapenv:Body>
<xsd:getAPIsForListing>
<!--Optional:-->
<xsd:pageNumber>0</xsd:pageNumber>
<!--Optional:-->
<xsd:itemsPerPage>10</xsd:itemsPerPage>
</xsd:getAPIsForListing>
</soapenv:Body>
[1] https://docs.wso2.com/display/EI610/Calling+Admin+Services+from+Apps#CallingAdminServicesfromApps-Discoveringtheadminservices

404 Not Found in Wso2apimanager

I created an api in wso2apimanager 1.7 and subscribed the created api through creating an application. But when i call the api using a REST client (postman) i'm getting 404 Not Found error.
cURL request
curl -X GET -H "Cache-Control: no-cache" -H "Authorization: Bearer f3afa04c46c95461ff2df4d037e9b2f" -H "Postman-Token: 8cb664dd-76ec-5ee1-40ee-ff27a1ce1942" "http://localhost:8280/test/path/V1/"
Response is
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
below is my api definition xml
<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse"
name="admin--TestingAPI"
context="/test/path"
version="V1"
version-type="url">
<resource methods="POST GET OPTIONS DELETE PUT"
url-mapping="/*"
faultSequence="fault">
<inSequence>
<send>
<endpoint name="admin--TestingAPI_APIproductionEndpoint_0">
<http uri-template="http://httpbin.org/ip"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
<handlers>
<handler class="org.wso2.carbon.apimgt.gateway.handlers.security.CORSRequestHandler"/>
</handlers>
</api>
I think the problem is with your backend service. It gives 404 when you try http://httpbin.org/ip/ instead of http://httpbin.org/ip. (note the slash at the end)
So if you send your request to below URL, it should work I think.
http://localhost:8280/test/path/V1
Looks like you have not used API Context and/or its resource name, Please give a try with that otherwise, please provide your API Definition with back-end API details for further help.
Follow this tutorial to get an idea how to create an API. It is for APIM 2.0.0 but the skills are transferable.

Error with WSO2 ESB when trying to connect to Jira and ElasticSearch

I have and scenario where I want to extract dynamically Issues from Jira in order to save them on ElasticSearch. I use a custom Proxy Service that connect with Jira (via Jira connector), get a project (first I want to save the project's information) and pass this information to Elastic. Following is the in Sequence of the proxy service:
<?xml version="1.0" encoding="UTF-8"?>
<inSequence xmlns="http://ws.apache.org/ns/synapse">
<property expression="json-eval($.username)" name="username"/>
<property expression="json-eval($.password)" name="password"/>
<property expression="json-eval($.uri)" name="uri"/>
<property expression="json-eval($.id)" name="id"/>
<property expression="json-eval($.uriEl)" name="uriEl"/>
<property expression="json-eval($.indexName)" name="indexName"/>
<property expression="json-eval($.type)" name="type"/>
<property expression="json-eval($.message)" name="message"/>
<jira.init>
<username>{$ctx:username}</username>
<password>{$ctx:password}</password>
<uri>{$ctx:uri}</uri>
</jira.init>
<jira.getProject>
<projectIdOrKey>{$ctx:id}</projectIdOrKey>
</jira.getProject>
<property expression="json-eval($.project)" name="project"
scope="default" type="STRING"/>
<elasticsearch.init>
<apiUrl>{$ctx:uriEl}</apiUrl>
</elasticsearch.init>
<elasticsearch.createDocumentWithIndex>
<indexName>{$ctx:indexName}</indexName>
<type>{$ctx:type}</type>
<message>{$ctx:message}</message>
<body>{$ctx:project}</body>
</elasticsearch.createDocumentWithIndex>
<respond/>
</inSequence>
And here is the sample request:
{
"username":"MyName",
"password":"AnStrongPassword",
"uri":"JiraURL",
"id":"MyProject",
"uriEl":"http://localhost:9200",
"indexName":"jira",
"type":"project",
"message":"Testing"
}
The other parameters (Endpoint, out Sequence and Fault Sequence) are by default.
The problem is that when I try to send this request via 'Try the Test service' option on WSO2 ESB, i get the following error:
Error connecting to the Tryit ajax proxy
I have tested the same code but only using the Jira connection (without get a project or connect to Elastic), and the same error occurs.
Looking to this error, I suppose that the problem is before to read the JSON request, but I don't know if it is related to the endpoint (I think that it is not necessary an endpoint for this scenario).
Thanks in advance.
This problem is totally independent of the jira connector etc. Problem is with the "try it" tool. This tool is not that much recommended for testing purposes. Instead of that, try invoking the proxy service via soap ui.
If you share a screenshot when the try it error occurs, I'll be able to comment on that as well.
After a few days I have found another solution to my problem without using proxy services. To connect ESB with Jira, I created this API on ESB:
<api xmlns="http://ws.apache.org/ns/synapse" name="transport_service" context="/transport_service">
<resource methods="GET">
<inSequence>
<jira.init>
<uri>MyURL</uri>
<username>MyUser</username>
<password>3MyPass</password>
</jira.init>
<jira.searchJira>
<query>project=MyProject</query>
<maxResults>50000</maxResults>
<fields>MyField1,MyField2,etc</fields>
</jira.searchJira>
<respond/>
</inSequence>
<outSequence>
<log level="full"/>
<property name="messageType" value="application/xml" scope="axis2"/>
<send/>
</outSequence>
</resource>
</api>
The command to call this API is simple:
curl -v -X GET http://localhost:8280/transport_service > jira-db.json
Finally, if you want to insert this json on elastic, you can follow the typical PUT via curl:
curl -XPUT 'http://localhost:9200/jira/jira/1' -d #jira-db.json
Additionally, you can write both commands on a daily bash script (using your scheduled task list), getting the same result that scheduling your proxy service on WSO2ESB:
#!/bin/bash
curl -v -X GET http://localhost:8280/transport_service > jira-db.json
curl -XPUT 'http://localhost:9200/jira-dev/jira/1' -d #jira-db.json
echo "Daily load completed"