# HTTP POST xml request with headers and authentification

**URL:** <https://discourse.julialang.org/t/http-post-xml-request-with-headers-and-authentification/61026>\
**Category:** General Usage\
**Tags:** http\
**Created:** [May 12, 2021, 2:58pm UTC](https://discourse.julialang.org/t/http-post-xml-request-with-headers-and-authentification/61026 "2021-05-12T14:58:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [May 12, 2021, 2:58pm UTC](https://discourse.julialang.org/t/http-post-xml-request-with-headers-and-authentification/61026/1 "2021-05-12T14:58:52Z")

</div>

Hi,

I have the following R code that I use to query an API:

```julia
httr::RETRY(verb="POST", url= "https://mywebsite.net/view", body = xml_payload,
                     httr::authenticate("myname","mypassword"), httr::content_type_xml(),
                     httr::use_proxy(""), httr::user_agent("api"), times = 1, quiet=FALSE)

```

I was wondering what is the equivalent Julia code using `HTTP.jl` (or else if you have an alternative method)?

I have tried:

```julia
HTTP.post("https://mywebsite.net/view", 
                 [["Content-Type" => "application/xml"],["userpwd" = > "myname:mypassword"]],xml_payload, proxy="", retries=1,verbose=1)

```

but I get the following error:

```julia
IOError(Base.IOError("connect: connection refused (ECONNREFUSED)",-4078) during request())

```

I am probably not writting the post equest in Julia correctly, any idea of the correct syntax?

Thank you  
Kind regards.

---

<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:** [May 12, 2021, 3:52pm UTC](https://discourse.julialang.org/t/http-post-xml-request-with-headers-and-authentification/61026/2 "2021-05-12T15:52:17Z")

</div>

You can compare the requests using `netcat`, e.g.

```sh
$ netcat -lp 8080

```

and send the requests to `http://localhost:8080`. That should give you an idea what the difference is.

* * *

> [@gitboy16](#):
>
> ```julia
> HTTP.post("https://mywebsite.net/view", 
> [["Content-Type" => "application/xml"],["userpwd" = > "myname:mypassword"]],xml_payload, proxy="", retries=1,verbose=1)
> 
> ```

This looks wrong though, the headers in the second argument should be an array of pairs, i.e.

```julia
["Content-Type" => "application/xml", "userpwd" => "myname:mypassword"]

```

However, do you really want to set a header called `userpwd` to that string? I would guess this uses HTTP basic authentication and you should instead include it in the URL,

```julia
HTTP.post("https://myname:mypassword@mywebsite.net", ...)

```

which HTTP.jl will rewrite to an `Authorization` header after encoding it, e.g.

```julia
HTTP.get("http://user:pass@localhost:8080")

```

gives

```julia
$ netcat -lp 8080
GET / HTTP/1.1
Authorization: Basic dXNlcjpwYXNz
Host: localhost:8080
Accept: */*
User-Agent: HTTP.jl/1.6.1
Content-Length: 0

```

Alternatively you can set the header yourself, e.g.

```julia
using HTTP, Base64
HTTP.get("http://localhost:8080", ["Authorization" => "Basic $(base64encode("user:pass"))")

```

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [May 12, 2021, 5:11pm UTC](https://discourse.julialang.org/t/http-post-xml-request-with-headers-and-authentification/61026/3 "2021-05-12T17:11:35Z")

</div>

Thanks @fredrikekre for the reply.  
Still not working and I don’t have `netcat`  
I think I have a problem of authentification…it seems the username and password should not be part of the headers but part of “Options” , is there a field argument in `HTTP.post` for that?

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [May 13, 2021, 10:44am UTC](https://discourse.julialang.org/t/http-post-xml-request-with-headers-and-authentification/61026/4 "2021-05-13T10:44:32Z")

</div>

I removed the proxy part and my error changed to :

```julia
ERROR: IOError (MbedTLS error code -9984: X509 - Certificate verification failed, e.g.CRL, CA or signature check failed during request

```

Any idea how to not verify?  
Thank you

---

<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:** [May 13, 2021, 10:50am UTC](https://discourse.julialang.org/t/http-post-xml-request-with-headers-and-authentification/61026/5 "2021-05-13T10:50:44Z")

</div>

> [@gitboy16](#):
>
> Still not working and I don’t have `netcat`

Well, can you install it? Alternatively give copy-pastable code so someone else can do it for you. You would probably have your answer to this problem in 10 seconds if you just inspect and compare the two requests.

> [@gitboy16](#):
>
> Any idea how to not verify?

The [documentation for HTTP.request](https://juliaweb.github.io/HTTP.jl/stable/public_interface/#HTTP.request) says you can use `require_ssl_verification = false`. HTTP.jl also respects settings from [NetworkOptions.jl](https://github.com/JuliaLang/NetworkOptions.jl#verify_host), so you could also set `JULIA_SSL_NO_VERIFY_HOSTS` as described there.

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [May 13, 2021, 11:03am UTC](https://discourse.julialang.org/t/http-post-xml-request-with-headers-and-authentification/61026/6 "2021-05-13T11:03:44Z")

</div>

Thank you @fredrikekre , that did the trick.  
No I can’t install it as I am running on corporate computer, nor can i copy paste it.  
Thank you very much. Slowly saying bye bye to R and welcome to Julia 🙂

---

<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:** [May 13, 2021, 11:48am UTC](https://discourse.julialang.org/t/http-post-xml-request-with-headers-and-authentification/61026/7 "2021-05-13T11:48:23Z")

</div>

> [@gitboy16](#):
>
> I can’t install it as I am running on corporate computer

Perhaps sending requests to [https://httpbin.org](https://httpbin.org) can be useful next time. For example [https://httpbin.org/headers](https://httpbin.org/#/Request_inspection/get_headers) and [https://httpbin.org/anything](https://httpbin.org/#/Anything/get_anything).

Alternatively, here is a Julia implemetation of the `netcat` functionality

```julia
using Sockets

s = Sockets.listen(Sockets.localhost, 8080);

while true
    c = accept(s)
    while isopen(c)
        write(stdout, c)
    end
end

```
