Ich kämpfe um eine ionische this.http.post
zu arbeiten.Curl POST funktioniert, aber ionic this.http.post nicht
Wenn ich diese curl in meinem Terminal verwenden es funktioniert super:
curl -v -X POST \
https://myuser-name:[email protected]:5984/_session \
-d 'name=app&password=ijF3Ui7VYVbbSejmwsnVVo'
Es gibt mir die folgende Ausgabe:
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
Mein ionischen POST-Code sieht wie folgt aus:
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);
});
});
}
Wenn ich versuche, die POST in ionischen ohne Auth in der URL mir eine sinnvolle Fehlermeldung:
Response with status: 401 Unauthorized for URL: https://app.mysite.com:5984/_session"
Aber wenn ich Auth an die URL hinzufügen Ich erhalte eine Fehlermeldung, die mir nicht sagen, was das Problem ist:
Response with status: 0 for URL: null
ich nicht herausfinden kann, warum es mit curl funktioniert, aber nicht innerhalb von ionischen http.post.
Ich habe das gleiche Problem, ob ich ionic serve
laufen oder ich die App auf einem iPhone laufen.
UPDATE
ich die ionische App in Chrome ausgeführt habe und habe jetzt einen besseren Fehler:
error: "unauthorized", reason: "Authentication required."
So ist es klar, dass ich nicht bin immer die POST-Anfrage korrekt aber nicht sehen kann, Warum.
Ihre HTTP-Anforderung durch curl und ionische sind unterschiedlich gesendet: Um sie zu erfüllen, kann
Authorization
Header manuell in HTTP-Anfrage hinzugefügt werden. Der "Content-Type" der HTTP-Anfrage, die von curl gesendet wird, ist 'application/x-www-form-urlencoded', die von ionic gesendete ist' application/json'.Können Sie sie konsistent machen und es erneut versuchen? – shaochuancs@schaochuancs Leider bekomme ich immer noch den gleichen Fehler. Ich habe meine Frage aktualisiert, um zu zeigen, dass ich in Chrome jetzt einen vernünftigen Fehler erhalte: Fehler: "nicht autorisiert", Grund: "Authentifizierung erforderlich." Also, ich formuliere meine POST-Anfrage nicht richtig. –
ja ... Sie haben es in eine Zeichenkette umgewandelt durch 'JSON.stringify (credentials) '. Aber es ist nicht notwendig, übergeben Sie einfach 'credential' als zweiten Parameter von' this.http.post' und es sollte OK sein. Ein offizielles Beispiel finden Sie unter https://angular.io/guide/http. – shaochuancs