2017-09-04 20 views
2

我努力获得离子this.http.post工作。卷曲POST作品,但离子这个.http.post不

如果我在终端使用卷曲它的伟大工程:

curl -v -X POST \ 
    https://myuser-name:[email protected]:5984/_session \ 
    -d 'name=app&password=ijF3Ui7VYVbbSejmwsnVVo' 

它给了我下面的输出:

Note: Unnecessary use of -X or --request, POST is already inferred. 
* Trying 37.1.96.50... 
* TCP_NODELAY set 
* Connected to app.mysite.com (37.1.96.49) port 5984 (#0) 
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 
* Server certificate: app.mysite.com 
* Server certificate: COMODO RSA Domain Validation Secure Server CA 
* Server certificate: COMODO RSA Certification Authority 
* Server auth using Basic with user 'myuser-name' 
> POST /_session HTTP/1.1 
> Host: app.mysite.com:5984 
> Authorization: Basic cDpkTUQySzg0a2lqRjNVaTdWWVZiYlNlam13c25WVm8= 
> User-Agent: curl/7.54.0 
> Accept: */* 
> Content-Length: 52 
> Content-Type: application/x-www-form-urlencoded 
> 
* upload completely sent off: 52 out of 52 bytes 
< HTTP/1.1 200 OK 
< Set-Cookie: AuthSession=ZWhzLWFwcDo1OUFENThGRjruBtcPzHcqc1sC9WXrcWI7R27_Mg; Version=1; Secure; Path=/; HttpOnly 
< Server: CouchDB/1.6.1 (Erlang OTP/18) 
< Date: Mon, 04 Sep 2017 13:45:35 GMT 
< Content-Type: text/plain; charset=utf-8 
< Content-Length: 43 
< Cache-Control: must-revalidate 
< 
{"ok":true,"name":null,"roles":["_admin"]} 
* Connection #0 to host app.mysite.com left intact 

我离子POST代码如下所示:

login(callerName:string):any 
 
    // Make sure we have a CouchDB session so that PouchDB can access the CouchDB database 
 
    { 
 
    console.log('Authentication: login(): Login function called from ' + callerName); 
 

 
    return new Promise(resolve => { 
 
     let headers = new Headers(); 
 
     headers.append('Content-Type', 'application/json'); 
 

 
     let credentials = { 
 
     name: COUCHDB_USER, 
 
     password: COUCHDB_PASSWORD 
 
     }; 
 

 
     let result = { 
 
     success: false, 
 
     data: [] 
 
     }; 
 

 
     console.log('Authentication: login(): credentials = ' + JSON.stringify(credentials)); 
 

 
     // NOTE: 
 
     // 
 
     // If POST is called with COUCHDB_SERVER with no auth in the url I get the error: Response with status: 401 Unauthorized for URL: https://app.mysite.com:5984/_session" 
 
     //  
 
     // If POST is called with COUCHDB_SERVER WITH auth in url I get the error: Response with status: 0 for URL: null 
 
     // This 'might' mean: 
 
     //  Timeout from server 
 
     //  Request not sent 
 
     //  Requesting an unreachable url 
 
     //  ... 
 
     // This WORKS with curl in terminal 
 
     // 
 
     // With auth in url: https://myuser-name:[email protected]:5984/_session 
 
     // Without auth in url: https://app.mysite.com:5984/_session 
 
     // 
 
     this.http.post(COUCHDB_SERVER + '/_session', JSON.stringify(credentials), {headers: headers}) 
 
     .subscribe(res => { 
 
      var details = res.json(); 
 
      console.log('Authentication: login(): SuperLogin successful login: res = ' + JSON.stringify(details)); 
 

 
      result.success = true; 
 
      result.data = details; 
 
      resolve(result); 
 
      }, 
 
      (err) => { 
 
      console.log('Authentication: login(): Login failed err = ' + err); 
 

 
      let details = err.json(); 
 
      result.success = false; 
 
      result.data = details; 
 
      resolve(result); 
 
      }); 
 
    }); 
 
    }

如果我尝试POST离子在URL中没有权威性,我得到一个合理的错误消息:

Response with status: 401 Unauthorized for URL: https://app.mysite.com:5984/_session" 

但是,如果我添加权威性的网址,我得到一个错误消息并没有告诉我什么问题是:

Response with status: 0 for URL: null 

我不能工作为什么它与curl工作,但不在离子http.post内。

我有同样的问题,无论我运行ionic serve还是我在iPhone上运行应用程序。

UPDATE

我已经运行在Chrome离子App和现在有一个更好的错误:

error: "unauthorized", reason: "Authentication required." 

所以很显然我没有收到POST请求正确的,但不能看为什么。

+1

由curl和ionic发送的HTTP请求是不同的。 curl发送的HTTP请求的'Content-Type'是'application/x-www-form-urlencoded',离子发送的是'application/json'。你可以让他们一致并再试一次吗? – shaochuancs

+0

@shaochuancs不幸的是我仍然得到同样的错误。我更新了我的问题,以显示在Chrome上我现在得到了一个明智的错误:错误:“未经授权”,原因:“需要身份验证。”所以很明显,我没有正确地形成我的POST请求 –

+1

是的...你已经通过'JSON.stringify(凭证)'将它转换为字符串。但没有必要,只需将'credential'作为this.http.post的第二个参数传递就可以了。官方示例请参阅https://angular.io/guide/http。 – shaochuancs

回答

3

由于this.http.post的使用不正确:第二个参数应该是HTTP请求正文对象(JSON,credential对象),而不是字符串,因此身份验证在离子中失败。例如,请参阅https://angular.io/guide/http

代码发送HTTP请求将是:

this.http.post(COUCHDB_SERVER + '/_session', credentials, {headers: headers})... 

它工作在卷曲,但不是在离子 - 那是因为由卷曲发送的HTTP请求的Content-Typeapplication/x-www-form-urlencoded,和卷曲的语法是正确的。

我应该在URL中添加auth吗? - 我想这意味着URL中的myuser-name:[email protected]部分。答案是否定:卷曲(在请求中添加Authorization标题),但在浏览器中不起作用,请检查Pass username and password in URL for HTTP Basic Auth了解详情。

更新:似乎在CouchDB中强制执行基本身份验证。为了满足它,可以在HTTP请求中手动添加Authorization标头:

headers.append('Authorization', 'Basic ' + window.btoa(username + ':' + password)) 
+0

我已经从网址中删除了身份验证,我的http.post现在按照您的示例。我现在正在弹出一个“需要身份验证”模式。 –

+0

我阅读Pass用户名的帖子,非常有用,谢谢。它解释了很多,我应该知道,但没有。特别是为什么卷曲的作品,但邮差没有。有趣的是,我无法让邮递员与我的POST通话一起工作。也许这是连接? –

+0

@BillNoble看起来您的CouchDB中启用了基本身份验证。为了满足弹出的“需要验证”模式,您可以手动添加离子代码中的Authorization标头。如:'headers.append('授权','基本'+ window.btoa(用户名+':'+密码))' – shaochuancs