简体   繁体   中英

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)?

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@email.com", "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@email.com", "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM