{"id":14972673,"url":"https://github.com/hemantasapkota/djangobot","last_synced_at":"2025-10-26T19:31:43.150Z","repository":{"id":57609320,"uuid":"83090668","full_name":"hemantasapkota/djangobot","owner":"hemantasapkota","description":"curl for django web framework. Make authenticated requests to a django server.","archived":false,"fork":false,"pushed_at":"2020-03-12T05:16:36.000Z","size":343,"stargazers_count":21,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-31T23:04:15.204Z","etag":null,"topics":["autocert","bot","csrf-tokens","curl","disqus","django","django-framework","django-server","go","golang","golang-library","golang-package","httpclient","sessionid"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hemantasapkota.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-24T23:11:27.000Z","updated_at":"2020-03-12T05:16:38.000Z","dependencies_parsed_at":"2022-08-27T10:40:47.782Z","dependency_job_id":null,"html_url":"https://github.com/hemantasapkota/djangobot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemantasapkota%2Fdjangobot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemantasapkota%2Fdjangobot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemantasapkota%2Fdjangobot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemantasapkota%2Fdjangobot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hemantasapkota","download_url":"https://codeload.github.com/hemantasapkota/djangobot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238394323,"owners_count":19464583,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autocert","bot","csrf-tokens","curl","disqus","django","django-framework","django-server","go","golang","golang-library","golang-package","httpclient","sessionid"],"created_at":"2024-09-24T13:47:18.523Z","updated_at":"2025-10-26T19:31:42.620Z","avatar_url":"https://github.com/hemantasapkota.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# djangobot\n[Curl](https://curl.haxx.se/) for [Django](https://www.djangoproject.com/). Make authenticated requests to a Django server.\n\n# How does it work ?\n\nDjango authentication relies on two cookies: **csrftoken** and **sessionid**. Once you accquire these cookies, you can make authenticated requests just like the browser does.\n\nGetting the **csrftoken** is easy. Just make a request to a page and the server sends back the cookie. \n\n**Sessionid**, however is tricky because most production servers configure it as a [secure HTTP only](https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-SESSION_COOKIE_SECURE) cookie. It is only sent if authentication is made securely ( via. HTTPS )\n\nTo make a secure connection we need SSL/TLS certificates. GO has a package called [autocert](https://godoc.org/golang.org/x/crypto/acme/autocert) which lets us accquire these certificates. Autocert provides automatic access to certificates from [Let's Encrypt](https://letsencrypt.org/) and any other ACME-based CA.\n\nWith certs in place, all we need is the authentication details and we're good to go.\n\n# What are the potential use cases for this library ?\n\n* API testing\n* Web Scraping\n* Automation\n* Bot frameworks\n* Mobile apps\n\nSee an example usage below.\n\n# Installation\n\n* Add ```github.com/hemantasapkota/djangobot``` as an import to your project.\n* Execute ```go get github.com/hemantasapkota/djangobot```\n\n# Usage\nIn this example, we'll authenticate with [Disqus](https://disqus.com/) which is built on top of Django. Let's inspect the parameters that get sent to the login endpoint.\n\n![](disqus.png)\n\nThe query parameter is **next** and the form data items are **csrfmiddlewaretoken**, **username**, and **password**.\n\nWe'll do the same. But before being able to call the login endpoint we'll need to accquire the CSRF token. Let's go get it.\n\n```go\nbot := djangobot.With(\"https://disqus.com/profile/login/\").\n\t\t ForHost(\"disqus.com\").\n\t\t SetUsername(\"\u003c\u003cusername\u003e\u003e\").\n\t\t SetPassword(\"\u003c\u003cpassword\u003e\u003e\").\n         \t LoadCookies()\n\nif bot.Error != nil {\n\tpanic(bot.Error)\n}\n```\n\nNext, let's authenticate with the server. Django expects the csrf token to be sent as the **csrfmiddlewaretoken** form data. **Set()** sets the query parameters and **X()** sets the form data.\n\n```go\nclient, err := bot.Set(\"next\", \"https://disqus.com/\").\n\t\t   X(\"csrfmiddlewaretoken\", bot.Cookie(\"csrftoken\").Value).\n\t\t   X(\"username\", bot.Username).\n\t\t   X(\"password\", bot.Password).\n\t\t   Login()\n\nif err != nil {\n\tpanic(err)\n}\n\nsessionid := bot.Cookie(\"sessionid\").Value\nif sessionid == \"\" {\n    panic(\"Authentication failed.\")\n}\n\n```\n\nSuccessful authentication creates the **sessionid** cookie and returns an http [client](https://github.com/parnurzeal/gorequest) object.\n\nFrom this point on, the HTTP client can be used to make requests. It's important to note that all subsequent requests should have at least these headers: **User-Agent**, **Referrer**, **X-CSRFToken**, and **X-Requested-With**.\n\nThe ```bot.Requester()``` method is available to prepare requests with pre-filled headers. Example below.\n\n### Changing your Discus account password\n\nLet's put this library to use by changing our account's password.\n\n```go\ndata := map[string]string{\n\t\"email\":        \"\u003c\u003cyour email address\u003e\u003e\",\n\t\"old_password\": \"\u003c\u003cyour old password\u003e\u003e\",\n\t\"password\":     \"\u003c\u003cnew password\u003e\u003e\",\n\t\"username\":     \"\u003c\u003cusername\u003e\u003e\",\n}\n\n_, body, _ := bot.Requester(\"PUT\", \"https://disqus.com/users/self/account/\").\n\t      Client.\n\t      Send(data).\n              End()\n\nfmt.Println(body)\n\n```\n\nPlease refer to the test file for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhemantasapkota%2Fdjangobot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhemantasapkota%2Fdjangobot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhemantasapkota%2Fdjangobot/lists"}