# HTTP: Login to MediaWiki API with post fails (works with python)

**URL:** https://discourse.julialang.org/t/http-login-to-mediawiki-api-with-post-fails-works-with-python/41657
**Category:** Web Stack
**Created:** [June 18, 2020, 9:39am UTC](https://discourse.julialang.org/t/http-login-to-mediawiki-api-with-post-fails-works-with-python/41657 "2020-06-18T09:39:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)
#### Post date: [June 18, 2020, 9:39am UTC](https://discourse.julialang.org/t/http-login-to-mediawiki-api-with-post-fails-works-with-python/41657/1 "2020-06-18T09:39:57Z")

</div>

Why can I login with python (module requests), but not with HTTP.jl?

The MediaWiki API requires a login token, a bot user name and password. See [python-code example](https://www.mediawiki.org/wiki/API:Login#POST_request). Bot user name looks like “user@juliahttp”. The password consists of numbers an ordinary characters.The same is true for the token, but it ends in “+\”. token: "“c6c2ae9a34ec56a212a1143ff440fb985eeb2049+\”  
So the REPL prints out:  
 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/5/a/5a58f077e12a9af212a43c9ff332979a23f16728.png)

The Python code reads:

```python
PARAMS_1 = {
    'action':"login",
    'lgname':bot,
    'lgpassword':botpwd,
    'lgtoken':LOGIN_TOKEN,
    'format':"json"
}

R = S.post(URL, data=PARAMS_1)

DATA = R.json()

```

And you get:  
`{'login': {'result': 'Success', 'lguserid': 3, 'lgusername': 'User'}}`

I did this in julia:

```julia
params = ["action=login",
          "lgname=$(bot)",
          "lgpassword=$(pwd)", 
          "lgtoken=$(logintoken)",
          "format=json"]

data = join(params, "&")
    
response = HTTP.request("POST", URL,
                        ["Content-Type" => "application/x-www-form-urlencoded"],
                        data)

```

I get a JSON response with a warning, that I should not try to get a token with `action=login`. So `data` is interpreted in some way. I get the same result, if I send a wrong password.

I applied `HTTP.URIs.escapeuri` to the bot user name and to the token. I get the same response. Thanks for a hint!

---

<div class="post-metadata">

### Author: ![gsoleilhac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gsoleilhac/32/3480_2.png) [@gsoleilhac](https://discourse.julialang.org/u/gsoleilhac)
#### Post date: [June 18, 2020, 10:23am UTC](https://discourse.julialang.org/t/http-login-to-mediawiki-api-with-post-fails-works-with-python/41657/2 "2020-06-18T10:23:34Z")

</div>

You probably want to send the data in the request body as JSON instead of a query string

```julia
using JSON
data = Dict("action" => "login",
    ...
    "format" => "json"
)

response = HTTP.post(URL,
    ["Content-Type" => "application/json"],
    JSON.json(data))

println(JSON.parse(String(response.body)))

```

---

<div class="post-metadata">

### Author: ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)
#### Post date: [June 18, 2020, 10:34am UTC](https://discourse.julialang.org/t/http-login-to-mediawiki-api-with-post-fails-works-with-python/41657/3 "2020-06-18T10:34:38Z")

</div>

Unfortunately the API does not accept such a request and responds with a default HTML error message.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [June 18, 2020, 11:20am UTC](https://discourse.julialang.org/t/http-login-to-mediawiki-api-with-post-fails-works-with-python/41657/4 "2020-06-18T11:20:29Z")

</div>

You can try to use the `query` keyword together with a `Dict` (much like your python code):

```julia
params = Dict(
    "action" => "login",
    "lgname" => bot,
    ...
)

HTTP.request("POST", URL; query=params)

```

---

<div class="post-metadata">

### Author: ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)
#### Post date: [June 18, 2020, 1:30pm UTC](https://discourse.julialang.org/t/http-login-to-mediawiki-api-with-post-fails-works-with-python/41657/5 "2020-06-18T13:30:32Z")

</div>

This is cool! However, it appends the parameters to the URL, but they are expected in the POST body in this case.

I set up a little server as described in the [documentation](https://juliaweb.github.io/HTTP.jl/stable/public_interface/#Server-/-Handlers-1) of HTTP.jl and saw that the python body and the julia body are the same. Then I set `cookies=true` in the request to get the login token, et voilà, that was it.

Some [background info](https://discourse.julialang.org/t/http-jl-managing-cookies-similar-to-python-requests/23665/5).

Thanks a lot for your comments!

---

<div class="post-metadata">

### Author: ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)
#### Post date: [May 18, 2022, 10:48am UTC](https://discourse.julialang.org/t/http-login-to-mediawiki-api-with-post-fails-works-with-python/41657/6 "2022-05-18T10:48:49Z")

</div>

You can post with `HTTP.Form(Dict())`
