![](/img/trans.png)
[英]Unable to send POST json request to restful api using Postman
[英]How to send POST request using Julia lang HTTP library to a Django RESTFUL API (DRF)
关于 Julia Lang 中的 HTTP 库的文档非常有限。 不仅如此,总体上还没有关于 HTTP 库的最新 Stack Overflow 问题。
话虽如此,您如何使用 Julia + HTTP 将 POST 请求发送到 Django Restful API (DRF)?
Julia 1.7, 1.8
如果您要发送 json 格式的数据(简单的 Django POST 请求):
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
如果您要发送 header 信息,例如身份验证令牌等。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.