2014-10-11 76 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