[英]HTTP POST call from Azure custom API Script not working
我有一個自定義的Azure API,我想執行一個HTTP POST請求。 在這種情況下使用Google Forms,但我認為這是一個普遍的問題。 我發現的是POST請求似乎已通過-Google表單獲得了響應-但沒有數據通過。
這是我的腳本:
exports.get = function(request, response) {
var p = request.parameters;
var item = JSON.stringify({entry_123456: "LOGIN", entry_234567: request.query.Email, entry_345678: request.query.Token });
require('request').post({
uri : 'https://docs.google.com/forms/d/myformkeyhere/formResponse',
headers : {'Content-Type': 'application/json'},
body : item
}, function(err, res, body){
if (err)
{
request.respond(500, "Error: " + err + "; item: " + item);
}
else
{
request.respond(200,"Success");
}
});
response.send(statusCodes.OK, { message : item + "; response=" + response });
};
我正在使用瀏覽器中的該地址對其進行測試:
https://myservice.azure-mobile.net/api/user?Email=testEmail@gmail.com&Token=YayToken
我收到以下回復:
{"message":"{\"entry_123456\":\"LOGIN\",\"entry_234567\":\"testEmail@gmail.com\",\"entry_345678\":\"YayToken\"}; response=[object Object]"}
我以為我已經按照文檔中的說明進行了操作,但是碰到了牆。 有人對此有經驗嗎?
謝謝!
在MSDN上得到了我的答案:
我查看了腳本,這些是我在腳本中發現的問題。
我不確定'request'客戶端是否支持'application / json'POST。 根據文檔,它們支持URL編碼。 https://github.com/mikeal/request#forms request.respond和response.send中存在一些混淆
感謝您創建Google表單。 我將URL加載到IE中,發現它們使用url編碼進行表單發布,並且應該可以使用請求庫來實現。
這是適合您的代碼。 您可以輕松地運行測試https://forumtest.azure-mobile.net/api/gdocs/?Data1=XYZ&Data2=ABC&Data3=BBC ,看到你以前的回復中輸入的變量https://docs.google.com / forms / d / 1DcvdfKSo4JK-9RFAdO5We8nYwT9e604V_HH1uHf8ges / viewanalytics?usp = form_confirm
exports.get = function(request, response) {
var googleRequest = require('request');
var googleUrl = 'https://docs.google.com/forms/d/1DcvdfKSo4JK-9RFAdO5We8nYwT9e604V_HH1uHf8ges/formResponse';
var r = googleRequest.post(
googleUrl,
{ form: { 'entry.1173958467':request.query.Data1,
'entry.240822405':request.query.Data2,
'entry.976020097':request.query.Data3 } },
function optionalCallback (err, httpResponse, body) {
if (err) {
console.error('upload failed:', err);
response.send(statusCodes.OK, { message : 'Failure' });
} else {
console.log('Upload successful! Server responded with:', body);
response.send(statusCodes.OK, { message : 'Success' });
}
});
};
希望這可以幫助。 Manesh Raveendran Terawe公司
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.