Koops
Koops

Reputation: 543

How to send POST request using Julia lang HTTP library to a Django RESTFUL API (DRF)

There is very limited documentation when it comes to the HTTP library in Julia Lang. Not just that, but there are no up to date Stack Overflow questions regarding the HTTP library in general.

With that said, how do you send POST requests using Julia + HTTP to a Django Restful API (DRF)?

Upvotes: 3

Views: 311

Answers (1)

Koops
Koops

Reputation: 543

Julia 1.7, 1.8

If you are sending json formatted data (simple Django POST request):

begin

using JSON
using HTTP

const url = "http://127.0.0.1:8000/api/profile"
payload = Dict("email" => "[email protected]", "password" => "12345password")

response = HTTP.request(
        "POST", url, ["Content-Type" => "application/json"], JSON.json(payload))

# this is necessary, JULIA discontinued python style Dictionaries
response = JSON.parse(String(response.body))
println(response)


end

If you are sending header information like Authentication tokens, etc.

begin

using JSON
using HTTP

const url = "http://127.0.0.1:8000/api/profile"
payload = Dict("email" => "[email protected]", "password" => "12345password")
access_token = "some access token"

headers = Dict(
        "Content-Type" => "application/json",
        "Authorization" => "Bearer $access_token")

response = HTTP.request(
        "POST", url, headers, JSON.json(payload))

# this is necessary, JULIA discontinued python style Dictionaries
response = JSON.parse(String(response.body))
println(response)


end

Upvotes: 3

Related Questions