Original Message:
Sent: Wed June 14, 2023 02:45 AM
From: Vedran Vidovic
Subject: How to send multipart/form-data using dp:url-open in XSLT.
@Mahender Batta it is great to hear that you solved this issue.
Just a note that could help other people with similar problems:
- in the first post the "multipart/form-data" was mentioned
- in the example solution the "application/x-www-form-urlencoded" is used
The first one is more complex and usually used to upload files so if the second option is supported it is good that you selected to use this option. You should be able to use both XSLT or GatewayScript to solve this problem.
The GateawayScript could for "application/x-www-form-urlencoded" be simplified. There is no need for "boundary" logic, just the body with username and password should be provided in that case (and the proper "content-type" header).
------------------------------
Vedran Vidovic
Original Message:
Sent: Wed June 14, 2023 02:07 AM
From: Mahender Batta
Subject: How to send multipart/form-data using dp:url-open in XSLT.
Dears,
Thanks for your response @Vedran Vidovic
I will look into your suggestion as well. However I solved it by using the XSLT. below is my code..
<xsl:variable name="username" select="'username'"/>
<xsl:variable name="password" select="'zMCZf0Lkk'"/>
<xsl:variable name="requestBody">
<xsl:text>username=</xsl:text>
<xsl:value-of select="$username"/>
<xsl:text>&password=</xsl:text>
<xsl:value-of select="$password"/>
</xsl:variable>
<xsl:variable name="url" select="'target url'"/>
<xsl:variable name="response">
<dp:url-open target="{$url}" response="responsecode-binary" content-type="application/x-www-form-urlencoded"
http-method="POST" data-type="base64">
<xsl:value-of select="dp:encode($requestBody, 'base-64')"/>
</dp:url-open>
</xsl:variable>
<xsl:copy-of select="dp:stringToJSONx(dp:decode(dp:binary-encode($response/result/binary/node()),'base-64'))"/>
------------------------------
Mahender Batta
Original Message:
Sent: Mon June 12, 2023 07:19 AM
From: Vedran Vidovic
Subject: How to send multipart/form-data using dp:url-open in XSLT.
Hi,
If you really need to send the multipart/form-data, this code is not preparing it correctly.
Instead of sending CRLF this example is sending literal characters "\" and "n".
Please check if the first "\r" is lost while creating this post or is it really missing in your code.
var crlf = '\\n'; -> var crlf = '\r\n';
The result of example request on the server side is is something like:
POST /v1/token HTTP/1.1
Connection: Keep-Alive
Content-Type: multipart/form-data; boundary=--------------------------275139195513885382856257 type="text/plain"
Host: 172.17.0.1:4000
Content-Length: 302
----------------------------275139195513885382856257\nContent-Disposition: form-data; name="username"\n\nusername\n----------------------------275139195513885382856257\nContent-Disposition: form-data; name="password"\n\nH0512!zMCZf0Lkk$HIWhcH6i\n----------------------------275139195513885382856257--\n
Instead of:
POST /v1/token HTTP/1.1
Connection: Keep-Alive
Content-Type: multipart/form-data; boundary=--------------------------275139195513885382856257 type="text/plain"
Host: 172.17.0.1:4000
Content-Length: 302
----------------------------275139195513885382856257
Content-Disposition: form-data; name="username"
username
----------------------------275139195513885382856257
Content-Disposition: form-data; name="password"
H0512!zMCZf0Lkk$HIWhcH6i
----------------------------275139195513885382856257--
------------------------------
Vedran Vidovic
Original Message:
Sent: Thu June 08, 2023 03:26 AM
From: Mahender Batta
Subject: How to send multipart/form-data using dp:url-open in XSLT.
Hi All,
I have encountered a case where i have to invoke my backend server which accepts multipart/form-data. Backend service is required two fields username and password.
I have tried using gatewayscript as below,
var urlopen = require('urlopen');
session.input.readAsBuffer(function (error, buffer){
if (error) {
session.output.write (error.errorMessage);
}
else {
var boundary = "--------------------------275139195513885382856257";
var crlf = '\\n';
var multipart = '--' + boundary + crlf +
'Content-Disposition: form-data; name="username"' + crlf + crlf +
'username' + crlf +
'--' + boundary + crlf +
'Content-Disposition: form-data; name="password"' + crlf + crlf +
'H0512!zMCZf0Lkk$HIWhcH6i' + crlf +
'--' + boundary + '--' + crlf;
var options = {
target: 'http://localhost:8090/v1/token',
method: 'post',
contentType: 'multipart/form-data; boundary=' + boundary + ' type="text/plain"',
timeout: 60,
data:multipart
};
urlopen.open(options, function(error, response) {
if (error) {
// an error occurred during the request sending or response header parsing
session.output.write("urlopen error: "+JSON.stringify(error));
} else {
// get the response status code
var responseStatusCode = response.statusCode;
var responseReasonPhrase = response.reasonPhrase;
console.log("Response status code: " + responseStatusCode);
console.log("Response reason phrase: " + responseReasonPhrase);
// reading response data
response.readAsBuffer(function(error, responseData){
if (error){
throw error ;
} else {
session.output.write(responseData) ;
}
});
}
});
}
}); When submit the request data:multipart which goes in the body to the backend server and returned with the error message says
usename can not be empty. Seems backend server is not looking into the payload body.
Am I missing anything here? Could you please suggest?
Thanks in advance.