2016-06-28 48 views
1

我試圖直接從NetSuite客戶端調用加拿大郵政API以獲得運費。從NetSuite客戶端調用CanadaPost

這是開發芻議測試代碼:

var url = 'https://ct.soa-gw.canadapost.ca/rs/ship/price'; 
var auth = 'Basic BASE64ENCODEDUSERNAMEANDPASSWORD'; 
var xml = '' + 
'<?xml version="1.0" encoding="utf-8"?>' + 
'<mailing-scenario xmlns="http://www.canadapost.ca/ws/ship/rate-v3">' + 
'<customer-number>1234567</customer-number>' + 
'<parcel-characteristics><weight>1.0</weight></parcel-characteristics>' + 
'<services><service-code>DOM.XP</service-code></services>' + 
'<origin-postal-code>K2B8J6</origin-postal-code>' + 
'<destination><domestic><postal-code>J0E1X0</postal-code></domestic></destination>' + 
'</mailing-scenario>'; 

var header = { 
    'Authorization': auth, 
    'Content-Type': 'application/soap+xml;application/vnd.cpc.ship.rate-v3+xml', 
    'Accept-language': 'en-CA', 
    'Accept': 'application/vnd.cpc.ship.rate-v3+xml' 
}; 

var r = nlapiRequestURL(url, xml, header, null, 'POST'); 

console.log(r.code[0]); 
console.log(r.body); 

的說明是很清楚和XML格式似乎是對的,但我不斷收到同樣的反應:

<?xml version="1.0" encoding="UTF-8"?> 
<messages xmlns="http://www.canadapost.ca/ws/messages"> 
<message> 
<code>400</code> 
<description>Backend Bad Request</description></message></messages> 

任何線索?

參考: https://www.canadapost.ca/cpo/mc/business/productsservices/developers/services/rating/getrates/default.jsf

*編輯:我也用 '的Content-Type' 的嘗試: '應用程序/ vnd.cpc.ship.rate-V3 + XML',我得到了以下錯誤:

<?xml version="1.0" encoding="UTF-8"?> 
<messages xmlns="http://www.canadapost.ca/ws/messages"> 
<message> 
<code>415</code> 
<description>Backend Unsupported Media Type</description></message></messages> 

回答

3

第一個問題是您的Content-Type標頭是錯誤的。你不能像這樣混合內容類型。在CP網站表示,它希望

Content-Type: "application/vnd.cpc.ship.rate-v3+xml" 

您的問題將是該NS明確指出,他們只會尊重某些內容類型值:

Also note that if you call nlapiRequestURL, passing in the header with a content type, NetSuite respects the following types:

all text media types (types starting with "text/")
"application/json"
"application/vnd.maxmind.com-country+json"
"application/xml"
"application/soap+xml"
"application/xhtml+xml"
"application/atom+xml"
Otherwise, NetSuite will overwrite the content type with our default type as if the type had not been specified. NetSuite default types are:

"text/xml; charset=UTF-8"
"application/x-www-form-urlencoded; charset=UTF-8"

我會做什麼將是檢驗是否CP讓你脫離Content-Type:「text/xml」或「application/xml」

如果不是,我會提交CP和NS的案例,希望CP可以允許更通用的類型或NS將允許一個特定的類型。別屏住呼吸。

什麼其實我已經做了,解決這樣的事情是隻代理通過我的控制系統的要求。編寫一個能接收請求的小應用非常簡單,正確重置標題並轉發請求。

+0

試圖改變內容類型,它給了我一個「後端不支持的媒體類型」錯誤。 該腳本當前正在通過PHP代理運行以處理憑據,但我們希望重構爲直接從客戶端運行。 – pipechang

+2

我想先與加拿大郵政進行技術支持。他們非常具體的內容類型要求沒有很好的理由。 Netsuite的限制沒有什麼好的理由,但是在那裏好運。 – bknights

+0

大聲笑...我輸入的完全一樣的問題,那就是StackOverflow上和喬爾Spoelsky是驚人的美麗... ...它給我在這裏... 香港專業教育學院已經提交了一份請求,加拿大郵政技術部&I將發佈(雙關語打算)的答案只要他們回到我身邊,因爲你知道我想要那些upvotes!大聲笑 – lopezdp