2014-10-11 77 views
0

我需要運行在雲代碼捲曲的要求,但我不能成功地將其轉換(返回錯誤代碼401:未經授權):轉換CURL請求Parse.com HttpWebRequest的雲代碼

捲曲請求格式是:

curl --digest -u Key:Secret "http://example.com/" --form [email protected]"animage.jpg" -X PUT 

我想這一點:

Parse.Cloud.httpRequest({ 
    method: 'PUT', 
    url: 'http://example.com', 
    headers: { 
    'Content-Type': "application/json", 
    'Key': "xXXXXx", 
    'Secret': "xXXXXXx", 
    }, 
body: { 
    'imagepath': "http://.../photo.png" 
}, 
success: function(httpResponse) { 

    console.log(httpResponse.text); 
}, 
error: function(httpResponse) { 

    console.error('Request failed with response code ' + httpResponse.status); 
} 
}); 

[更新]

我試着用GET方法(要了解它是如何工作),這正與該呼叫(我找到了與谷歌的Chrome瀏覽器開發檢查員授權線...):

Parse.Cloud.httpRequest({ 
method: 'GET', 
url: 'http://example.com', 

headers: { 
Authorization: 'Digest username="XXxxXXXX", realm="API Name", nonce="XXXxxxXXX",  uri="example.com", response="XXXxxxXXX", opaque="XXxxxxXXX", qop=auth, nc=xxXXXxx,  cnonce="XXXxxxXX"' 
}, 

success: function(httpResponse) { 

console.log(httpResponse.text); 
response.success("Okayyy!"); 
}, 
error: function(httpResponse) { 

console.error('Request failed with response code ' + httpResponse.status); 

response.error("error"); 
} 
}); 

但是沒有辦法讓PUT方法作品。

回答

0

它看起來像你正試圖加載具有基本身份驗證的URL。如果這是真的,您可以將憑據作爲URL的一部分傳遞。

Parse.Cloud.httpRequest({ 
    method: 'PUT', 
    url: 'http://key:[email protected]', 
    headers: { 
    'Content-Type': "application/json", 
    }, 
body: { 
    'imagepath': "http://.../photo.png" 
}, 
success: function(httpResponse) { 

    console.log(httpResponse.text); 
}, 
error: function(httpResponse) { 

    console.error('Request failed with response code ' + httpResponse.status); 
} 
}); 
+0

感謝。我試過你的解決方案,但給我一個400錯誤代碼(壞請求)。 – oliver 2014-10-11 20:59:42

+0

我注意到你的curl命令使用了PUT,但是你的代碼使用了POST。我已經改變了代碼,試試這個。 – sarvesh 2014-10-12 19:18:13

+0

是的,我已經注意到這個錯字,用PUT嘗試沒有成功:'(的迴應是:‘請求響應代碼失敗400’ – oliver 2014-10-12 20:48:37