HTTP POST xml request with headers and authentification

Hi,

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

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:

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:

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.

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

$ netcat -lp 8080

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


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

["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,

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

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

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

gives

$ 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.

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

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?

I removed the proxy part and my error changed to :

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

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.

The documentation for HTTP.request says you can use require_ssl_verification = false. HTTP.jl also respects settings from NetworkOptions.jl, so you could also set JULIA_SSL_NO_VERIFY_HOSTS as described there.

1 Like

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 :slight_smile:

1 Like

Perhaps sending requests to https://httpbin.org can be useful next time. For example https://httpbin.org/headers and https://httpbin.org/anything.

Alternatively, here is a Julia implemetation of the netcat functionality

using Sockets

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

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