Highlighted

Passing XML code to Get Repose from API using Coldfusion

Explorer ,
Oct 08, 2017

Copy link to clipboard

Copied

I had the following PHP code that posts XML data and retrieves the status as successful post or not This is a valid code and running fine using PHP. This code was sent by the company to test the api, but as the development is done using ColdFusion I wrote a code, given below in ColdFusion, Its sends an OK response but returns an error code as per behavior or the API.

PHP Code:

<?php
define
('blueEx', "http://benefit.blue-ex.com/api/post.php");
$xml
= '<?xml version="1.0" encoding="utf-8"?>
<BenefitDocument>
<AccessRequest>
<DocumentType>1</DocumentType>
<TestTransaction>Y</TestTransaction>*For Testing purpose, after testing leave it blank
<ShipmentDetail>
  
<ShipperName></ShipperName>
  
<ShipperAddress></ShipperAddress>
  
<ShipperContact></ShipperContact>
  
<ShipperEmail></ShipperEmail>
  
<ConsigneeName>Tayyab</ConsigneeName>
  
<ConsigneeAddress>202-N,DHA,Lahore</ConsigneeAddress>
  
<ConsigneeContact>03004306499</ConsigneeContact>
  
<ConsigneeEmail>fusioner@gmail.com</ConsigneeEmail>
  
<CollectionRequired>Y</CollectionRequired>
  
<ProductDetail>Some Product</ProductDetail>
  
<ProductValue>200</ProductValue>
  
<OriginCity>ISB</OriginCity>
  
<DestinationCountry>PK</DestinationCountry>
  
<DestinationCity>LHE</DestinationCity>
  
<ServiceCode>BG</ServiceCode>
  
<ParcelType>P</ParcelType>
  
<Peices>1</Peices>
  
<Weight>1</Weight>
  
<Fragile>N</Fragile>
  
<ShipperReference>9010191</ShipperReference>
  
<InsuranceRequire>N</InsuranceRequire>
  
<InsuranceValue></InsuranceValue>
  
<ShipperComment>xxxx</ShipperComment>
</ShipmentDetail>
</AccessRequest>
</BenefitDocument>';
$c = curl_init(); 
  curl_setopt($c, CURLOPT_URL, blueEx );
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1 );
  curl_setopt($c, CURLOPT_USERPWD, "sphere.technologies:sphere.tech");
  curl_setopt($c, CURLOPT_POST, 1 );
  curl_setopt($c, CURLOPT_POSTFIELDS, array('xml'=>$xml) );
  curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type=application/soap+xml', 'charset=utf-8'));
  $result = curl_exec ($c);
  $xres = simplexml_load_string($result);
  $st = $xres->status;
  $no = $xres->message;

  echo $st . ' - ' . $no;
?>

ColdFusion Code

<cfset objCFHttpProperties = {
  
Useragent = "#CGI.http_user_agent#",
  
Username = "sphere.technologies",
  
Password = "sphere.tech"
  }
/>
<cfxml variable="xmlString">
  
<BenefitDocument>
  
<AccessRequest>
  
<DocumentType>1</DocumentType>
  
<TestTransaction>Y</TestTransaction>
  
<ShipmentDetail>
  
<ShipperName></ShipperName>
  
<ShipperAddress></ShipperAddress>
  
<ShipperContact></ShipperContact>
  
<ShipperEmail></ShipperEmail>
  
<ConsigneeName>Tayyab</ConsigneeName>
  
<ConsigneeAddress>202-N,DHA,Lahore</ConsigneeAddress>
  
<ConsigneeContact>03004306499</ConsigneeContact>
  
<ConsigneeEmail>fusioner@gmail.com</ConsigneeEmail>
  
<CollectionRequired>Y</CollectionRequired>
  
<ProductDetail>Some Product</ProductDetail>
  
<ProductValue>200</ProductValue>
  
<OriginCity>ISB</OriginCity>
  
<DestinationCountry>PK</DestinationCountry>
  
<DestinationCity>LHE</DestinationCity>
  
<ServiceCode>BG</ServiceCode>
  
<ParcelType>P</ParcelType>
  
<Peices>1</Peices>
  
<Weight>1</Weight>
  
<Fragile>N</Fragile>
  
<ShipperReference>9010191</ShipperReference>
  
<InsuranceRequire>N</InsuranceRequire>
  
<InsuranceValue></InsuranceValue>
  
<ShipperComment></ShipperComment>
  
</ShipmentDetail>
  
</AccessRequest>
</BenefitDocument>
</cfxml>
<cfhttp url="http://benefitx.blue-ex.com/api/post.php" method="post" result="result" attributecollection="#objCFHttpProperties#">
  
<cfhttpparam type="header" name="Content-Type" value="application/soap+xml"/>
  
<cfhttpparam type="xml" name="xmlString" value="#xmlString#" />
</cfhttp>
<cfdump var="#result#">

But this code returns the error Status

I've been trying to resolve the issue but cannot come to resolve. Can Someone Help, where I went wrong.

Thanks

Adobe Community Professional
Correct answer by BKBK | Adobe Community Professional

Send no header (letting ColdFusion decide which to send) and send the XML as a form field.

<cfhttp url="http://benefitx.blue-ex.com/api/post.php" method="post" result="result" attributecollection="#objCFHttpProperties#">

   <cfhttpparam type="formfield" name="xml" value="#xmlString#" />

</cfhttp>

Views

810

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Passing XML code to Get Repose from API using Coldfusion

Explorer ,
Oct 08, 2017

Copy link to clipboard

Copied

I had the following PHP code that posts XML data and retrieves the status as successful post or not This is a valid code and running fine using PHP. This code was sent by the company to test the api, but as the development is done using ColdFusion I wrote a code, given below in ColdFusion, Its sends an OK response but returns an error code as per behavior or the API.

PHP Code:

<?php
define
('blueEx', "http://benefit.blue-ex.com/api/post.php");
$xml
= '<?xml version="1.0" encoding="utf-8"?>
<BenefitDocument>
<AccessRequest>
<DocumentType>1</DocumentType>
<TestTransaction>Y</TestTransaction>*For Testing purpose, after testing leave it blank
<ShipmentDetail>
  
<ShipperName></ShipperName>
  
<ShipperAddress></ShipperAddress>
  
<ShipperContact></ShipperContact>
  
<ShipperEmail></ShipperEmail>
  
<ConsigneeName>Tayyab</ConsigneeName>
  
<ConsigneeAddress>202-N,DHA,Lahore</ConsigneeAddress>
  
<ConsigneeContact>03004306499</ConsigneeContact>
  
<ConsigneeEmail>fusioner@gmail.com</ConsigneeEmail>
  
<CollectionRequired>Y</CollectionRequired>
  
<ProductDetail>Some Product</ProductDetail>
  
<ProductValue>200</ProductValue>
  
<OriginCity>ISB</OriginCity>
  
<DestinationCountry>PK</DestinationCountry>
  
<DestinationCity>LHE</DestinationCity>
  
<ServiceCode>BG</ServiceCode>
  
<ParcelType>P</ParcelType>
  
<Peices>1</Peices>
  
<Weight>1</Weight>
  
<Fragile>N</Fragile>
  
<ShipperReference>9010191</ShipperReference>
  
<InsuranceRequire>N</InsuranceRequire>
  
<InsuranceValue></InsuranceValue>
  
<ShipperComment>xxxx</ShipperComment>
</ShipmentDetail>
</AccessRequest>
</BenefitDocument>';
$c = curl_init(); 
  curl_setopt($c, CURLOPT_URL, blueEx );
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1 );
  curl_setopt($c, CURLOPT_USERPWD, "sphere.technologies:sphere.tech");
  curl_setopt($c, CURLOPT_POST, 1 );
  curl_setopt($c, CURLOPT_POSTFIELDS, array('xml'=>$xml) );
  curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type=application/soap+xml', 'charset=utf-8'));
  $result = curl_exec ($c);
  $xres = simplexml_load_string($result);
  $st = $xres->status;
  $no = $xres->message;

  echo $st . ' - ' . $no;
?>

ColdFusion Code

<cfset objCFHttpProperties = {
  
Useragent = "#CGI.http_user_agent#",
  
Username = "sphere.technologies",
  
Password = "sphere.tech"
  }
/>
<cfxml variable="xmlString">
  
<BenefitDocument>
  
<AccessRequest>
  
<DocumentType>1</DocumentType>
  
<TestTransaction>Y</TestTransaction>
  
<ShipmentDetail>
  
<ShipperName></ShipperName>
  
<ShipperAddress></ShipperAddress>
  
<ShipperContact></ShipperContact>
  
<ShipperEmail></ShipperEmail>
  
<ConsigneeName>Tayyab</ConsigneeName>
  
<ConsigneeAddress>202-N,DHA,Lahore</ConsigneeAddress>
  
<ConsigneeContact>03004306499</ConsigneeContact>
  
<ConsigneeEmail>fusioner@gmail.com</ConsigneeEmail>
  
<CollectionRequired>Y</CollectionRequired>
  
<ProductDetail>Some Product</ProductDetail>
  
<ProductValue>200</ProductValue>
  
<OriginCity>ISB</OriginCity>
  
<DestinationCountry>PK</DestinationCountry>
  
<DestinationCity>LHE</DestinationCity>
  
<ServiceCode>BG</ServiceCode>
  
<ParcelType>P</ParcelType>
  
<Peices>1</Peices>
  
<Weight>1</Weight>
  
<Fragile>N</Fragile>
  
<ShipperReference>9010191</ShipperReference>
  
<InsuranceRequire>N</InsuranceRequire>
  
<InsuranceValue></InsuranceValue>
  
<ShipperComment></ShipperComment>
  
</ShipmentDetail>
  
</AccessRequest>
</BenefitDocument>
</cfxml>
<cfhttp url="http://benefitx.blue-ex.com/api/post.php" method="post" result="result" attributecollection="#objCFHttpProperties#">
  
<cfhttpparam type="header" name="Content-Type" value="application/soap+xml"/>
  
<cfhttpparam type="xml" name="xmlString" value="#xmlString#" />
</cfhttp>
<cfdump var="#result#">

But this code returns the error Status

I've been trying to resolve the issue but cannot come to resolve. Can Someone Help, where I went wrong.

Thanks

Adobe Community Professional
Correct answer by BKBK | Adobe Community Professional

Send no header (letting ColdFusion decide which to send) and send the XML as a form field.

<cfhttp url="http://benefitx.blue-ex.com/api/post.php" method="post" result="result" attributecollection="#objCFHttpProperties#">

   <cfhttpparam type="formfield" name="xml" value="#xmlString#" />

</cfhttp>

Views

811

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 08, 2017 0
Advocate ,
Oct 11, 2017

Copy link to clipboard

Copied

It's just a guess, but try changing this line:

<cfhttpparam type="xml" name="xmlString" value="#xmlString#" />

to:

<cfhttpparam type="xml" name="xmlString" value="#toString(xmlString)#" />

and see if anything changes.

Cheers

Eddie

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 11, 2017 1
Explorer ,
Oct 11, 2017

Copy link to clipboard

Copied

Hello,

I tried it in the very beginning, but it did not respond accordingly, Gave the same results.

Regards

Tayyab Hussain

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 11, 2017 0
Advocate ,
Oct 11, 2017

Copy link to clipboard

Copied

A useful troubleshooting step would be to create your own ColdFusion script and change the posting URL (http://benefit.blue-ex.com/api/post.php) to your own script. Then do the post from the PHP code and observe what your script receives, then do the post from your CF code and see what your script receives.

When you compare the results you will be able to see whether you are doing the same thing in CF as you are doing in PHP.

Cheers

Eddie

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 11, 2017 0
Adobe Community Professional ,
Oct 14, 2017

Copy link to clipboard

Copied

Send no header (letting ColdFusion decide which to send) and send the XML as a form field.

<cfhttp url="http://benefitx.blue-ex.com/api/post.php" method="post" result="result" attributecollection="#objCFHttpProperties#">

   <cfhttpparam type="formfield" name="xml" value="#xmlString#" />

</cfhttp>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 14, 2017 1
Explorer ,
Oct 14, 2017

Copy link to clipboard

Copied

Dear BKBK,

Thanks for the the help.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 14, 2017 0
Explorer ,
Oct 14, 2017

Copy link to clipboard

Copied

Dear BKBK,

Can I know the logic  behind NOT sending the header, It will be an addition to my knowledge. By the way, I was looking forward for your help, as in the past whenever I got stuck, your solutions were always very helpful and correct.

Regards

Tayyab Hussain

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 14, 2017 0
Adobe Community Professional ,
Oct 14, 2017

Copy link to clipboard

Copied

1) Why I thought of a form field:

The line curl_setopt($c, CURLOPT_POSTFIELDS, array('xml'=>$xml)); posts the data as a form field. You say that this code works, so I took it to be the proof-of-concept.

2) Why I omitted the Content-type header:

In my experience, cfhttp posts can fail because of this header. So, I leave it out, unless it is absolutely necessary.

To give you an example of the complications that may arise, see my remarks in this previous thread. It turns out that the header may have the name Content-Type or Content_Type. If the one doesn't work, you could try the other. Therefore, chances are, the following might work for you:

<cfhttp url="http://benefitx.blue-ex.com/api/post.php" method="post" result="result" attributecollection="#objCFHttpProperties#">

   <cfhttpparam type="Header" name="Content_Type" value="application/soap+xml"/>

   <cfhttpparam type="formfield" name="xml" value="#xmlString#" />

</cfhttp>

<cfhttp url="http://benefitx.blue-ex.com/api/post.php" method="post" result="result" attributecollection="#objCFHttpProperties#">

   <cfhttpparam type="CGI" name="Content_Type" value="application/soap+xml"/>

   <cfhttpparam type="formfield" name="xml" value="#xmlString#" />

</cfhttp>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 14, 2017 1
Explorer ,
Oct 16, 2017

Copy link to clipboard

Copied

Thanks BKBK, That was very Helpful

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 16, 2017 0