Skip to content

DioError [DioErrorType.RESPONSE]: Http status error [401], but I am authorized #801

Closed

Description

@klnfreedom

Hello, I have some problem with create a document on appwrite (flutter web), when I start the process for creation, I have an error: DioError [DioErrorType.RESPONSE]: Http status error [401].
But I am authorized.
And I can read a document from appwrite, but I can't create a document to appwrite
Thats a part of code of creation document:

Future uploadDataToAppwrite(GasStationFromTransnet data) async {
var qwert = jsonEncode(data.toJson());
try {
Response response = await database.createDocument(
collectionId: AppConstants.collection,
data: data.toJson(),
read: [""],
write: [""],
);
print(response);
return true;
} catch (e) {
print(e);
return false;
}
}
Response is null.

Please help


Appwrite Version: 0.6.2.125

Activity

  1. eldadfux commented on Jan 5, 2021

    @eldadfux
    Member

    Before creating documents you need to authenticate your user by creating an account, and then an account session.

  2. klnfreedom commented on Jan 5, 2021

    @klnfreedom
    Author

    {"$id":"5ff46a34a631f","type":1,"expire":1641389492}
    [GETX] "GetMaterialController" has been initialized
    [GETX] GOING TO ROUTE /home
    [GETX] "HomeController" has been initialized
    DioError [DioErrorType.RESPONSE]: Http status error [401]
    false

    I made it, but not working, how can I fix it?

  3. klnfreedom commented on Jan 5, 2021

    @klnfreedom
    Author

    The same error

  4. klnfreedom commented on Jan 5, 2021

    @klnfreedom
    Author

    I'm authenticated, but the same error

  5. eldadfux commented on Jan 5, 2021

    @eldadfux
    Member

    Make sure your collection has write permissions for that user, * can be a good value for testing

  6. klnfreedom commented on Jan 5, 2021

    @klnfreedom
    Author

    When I want to create document, I need to take json code from collection or from the document?

  7. eldadfux commented on Jan 5, 2021

    @eldadfux
    Member

    @klnfreedom not sure I understand the question. Do you have a wildcard permission in your collection settings?

    image

  8. klnfreedom commented on Jan 5, 2021

    @klnfreedom
    Author

    Yes, I have a wildcard permission in write access and read accsess too

  9. klnfreedom commented on Jan 5, 2021

    @klnfreedom
    Author

    Screenshot_1

  10. klnfreedom commented on Jan 5, 2021

    @klnfreedom
    Author

    Is it a right json code for creating document?

    class Autogenerated {
    String id;
    String collection;
    Permissions permissions;
    String date;

    Autogenerated({this.id, this.collection, this.permissions, this.date});

    Autogenerated.fromJson(Map<String, dynamic> json) {
    id = json['$id'];
    collection = json['$collection'];
    permissions = json['$permissions'] != null
    ? new Permissions.fromJson(json['$permissions'])
    : null;
    date = json['Date'];
    }

    Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['Date'] = this.date;
    return data;
    }
    }

    class Permissions {
    List read;
    List write;

    Permissions({this.read, this.write});

    Permissions.fromJson(Map<String, dynamic> json) {
    read = json['read'].cast();
    write = json['write'].cast();
    }

    Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['read'] = this.read;
    data['write'] = this.write;
    return data;
    }
    }

  11. klnfreedom commented on Jan 5, 2021

    @klnfreedom
    Author

    When I want read document from appwrite, I can do it, but when I want to create document to appwrite, I can't do it. What I need to do? Create session is success but I can' to write.

  12. klnfreedom commented on Jan 5, 2021

    @klnfreedom
    Author

    try {
    Response response = await database.createDocument(
    collectionId: AppConstants.collection,
    data: data.toJson(),
    read: [""],
    write: ["
    "],
    );
    print(response);
    return true;
    } catch (e) {
    print(e);
    return false;
    }
    }
    It's a part of my code for creating

  13. klnfreedom commented on Jan 5, 2021

    @klnfreedom
    Author

    what am I doing wrong?

  14. eldadfux commented on Jan 5, 2021

    @eldadfux
    Member

    Are you able to fetch your user account? you can only write the document after creating a user account + login (creating a user session). If you can't get the user account your session might have been deleted or expired.

  15. klnfreedom commented on Jan 5, 2021

    @klnfreedom
    Author

    I can't get session, because I get the same error
    [GETX] "GetMaterialController" has been initialized
    [GETX] GOING TO ROUTE /home
    [GETX] "HomeController" has been initialized
    {"$id":"5ff48213a6a04","type":1,"expire":1641395603} - init client()
    DioError [DioErrorType.RESPONSE]: Http status error [401] - getSession
    DioError [DioErrorType.RESPONSE]: Http status error [401] - try to create document

  16. eldadfux commented on Jan 6, 2021

    @eldadfux
    Member

    @klnfreedom you need to use createSession not getSession

  17. klnfreedom commented on Jan 6, 2021

    @klnfreedom
    Author

    initClient it is a method which is included createSession();

    It is very interesting, I can create session and I get id of client, but when I want to getSession, I give DioException Http 401

    And when I want to create document I get the same problem

  18. eldadfux commented on Jan 7, 2021

    @eldadfux
    Member

    @klnfreedom are you testing this on Flutter Web?

  19. klnfreedom commented on Jan 7, 2021

    @klnfreedom
    Author

    Yes, I tested, but I have the same error

  20. eldadfux commented on Jan 7, 2021

    @eldadfux
    Member

    @klnfreedom sorry for all the questions, but I just want to be sure I understand the problem scope. This error happens to you only on web or web+ios+android?

  21. klnfreedom commented on Jan 8, 2021

    @klnfreedom
    Author

    Everythhing is cool, thank you for your support

  22. Macdeo commented on Jan 16, 2021

    @Macdeo

    How did you fixed this issue @klnfreedom

  23. klnfreedom commented on Jan 16, 2021

    @klnfreedom
    Author

    How did you fixed this issue @klnfreedom

    appwrite/sdk-for-flutter#5

  24. Macdeo commented on Jan 16, 2021

    @Macdeo

    Thanks

  25. lohanidamodar commented on Jan 20, 2021

    @lohanidamodar
    Member

    I think it's safe to close this issue as it is for now resolved by
    appwrite/sdk-for-flutter#5
    Feel free to reopen if it is not resolved for you, or continue discussion in appwrite/sdk-for-flutter#5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions