Search code examples
wso2

How do I make a post request to an external link in wso2 through an api with a GET call that only takes the alias and value?


I wish to create an API in publisher, where in a GET request i provide the alias and value as queryParams, the API will then make the post request to an external link, with basic Auth and other values that are already provided. The goal is to just make the whole process easier and so i don't have to directly make the POST request directly to the link. nFor context my wso2 server is deployed on my own server, instead of localhost, but when i want to deploy it i only have the localhost gateway

I created the API in publisher, created the resource with alias and value queryParams, on the endpoint, i clicked on mock implementation and asked chatGPT for the code, i got this :

var password = 'your_password';
var externalUrl = 'https://your_external_url';

var value = mc.getProperty('query.param.value');
var alias = mc.getProperty('query.param.alias');

if (!value || !alias) {
    mc.setProperty('HTTP_SC', '400');
    mc.setPayloadJSON({ error: 'Both value and alias are required.' });
} else {
    // Construct the HTTP request payload
    var payload = JSON.stringify({ value: value, alias: alias });

    // Set HTTP headers for basic authentication
    var headers = {};
    headers['Authorization'] = 'Basic ' + getBase64EncodedCredentials(username, password);
    headers['Content-Type'] = 'application/json';

    // Send the HTTP request
    sendTo({
        endpoint: externalUrl,
        method: 'POST',
        headers: headers,
        payload: payload,
        onSuccess: function (response) {
            // Set HTTP status code and response payload
            mc.setProperty('HTTP_SC', '200');
            mc.setPayloadJSON(response);
        },
        onError: function (error) {
            // Set HTTP status code and error message
            mc.setProperty('HTTP_SC', '500');
            mc.setPayloadJSON({ error: 'An error occurred while processing your request.' });
        }
    });
}

// Function to encode username and password as base64
function getBase64EncodedCredentials(username, password) {
    return Buffer.from(username + ':' + password).toString('base64');
}```
Although i'm not sure if it will work.

I deploy it with only localhost available as a gateway, and when i try it on devportal i got :

'Failed to fetch' when i request with localhost and when i try to request with the IP of my server, on the browser i get  'Error 405 - Method not Allowed'.

Am i doing it the wrong way, i don't think the issue is with code provided here as i don't even get to make the call, please help me 


Solution

  • Your custom mediation policy should look like something below.

    <sequence name="custom-sequence" xmlns="http://ws.apache.org/ns/synapse">
    
        <header name="Authorization" expression="fn:concat('Basic ', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx')" scope="transport" />
    
        <payloadFactory media-type="json">
            <format>{ value: $1, alias: $2</format>
            <args>
                <arg evaluator="xml" expression="get-property('query.param.value')" />
                <arg evaluator="xml" expression="get-property('query.param.alias')" />
            </args>
        </payloadFactory>
        <property name="HTTP_METHOD" scope="axis2" type="STRING" value="POST"/>
    </sequence>
    

    Rather than adding the Authorization header from the sequence you should be able to use Endpoint Security as well.