return application/json content-type for post api

This commit is contained in:
aglkm
2024-07-10 18:47:43 +03:00
parent ac27c5a064
commit 3e9648bf9c
4 changed files with 21 additions and 21 deletions

1
Cargo.lock generated
View File

@@ -1834,6 +1834,7 @@ dependencies = [
"rocket_codegen", "rocket_codegen",
"rocket_http", "rocket_http",
"serde", "serde",
"serde_json",
"state", "state",
"tempfile", "tempfile",
"time", "time",

View File

@@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
rocket = "0.5.0" rocket = {version = "0.5.0", features = ["json"]}
serde = {version = "1.0.198", features = ["derive"]} serde = {version = "1.0.198", features = ["derive"]}
serde_json = "1.0.111" serde_json = "1.0.111"
num-format = "0.4.4" num-format = "0.4.4"

View File

@@ -1,12 +1,11 @@
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
use rocket_dyn_templates::Template; use rocket_dyn_templates::{Template, context};
use rocket_dyn_templates::context;
use rocket::fs::FileServer; use rocket::fs::FileServer;
use rocket::State; use rocket::{State, tokio};
use rocket::serde::json::Json;
use rocket::response::Redirect;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use rocket::tokio;
use rocket::response::Redirect;
use either::Either; use either::Either;
use serde_json::Value; use serde_json::Value;
@@ -229,18 +228,18 @@ pub async fn search(input: Option<&str>) -> Either<Template, Redirect> {
// Owner API. // Owner API.
// Whitelisted methods: get_connected_peers, get_peers, get_status. // Whitelisted methods: get_connected_peers, get_peers, get_status.
#[post("/v2/owner", data="<data>")] #[post("/v2/owner", data="<data>")]
async fn api_owner(data: &str) -> String { async fn api_owner(data: &str) -> Json<String> {
if CONFIG.public_api == "enabled" { if CONFIG.public_api == "enabled" {
let result = serde_json::from_str(data); let result = serde_json::from_str(data);
let v: Value = match result { let v: Value = match result {
Ok(value) => value, Ok(value) => value,
Err(_err) => return "{\"error\":\"bad syntax\"}".to_string(), Err(_err) => return Json("{\"error\":\"bad syntax\"}".to_string()),
}; };
let method = match v["method"].as_str() { let method = match v["method"].as_str() {
Some(value) => value, Some(value) => value,
_ => return "{\"error\":\"bad syntax\"}".to_string(), _ => return Json("{\"error\":\"bad syntax\"}".to_string()),
}; };
if method == "get_connected_peers" || method == "get_peers" || method == "get_status" { if method == "get_connected_peers" || method == "get_peers" || method == "get_status" {
@@ -248,15 +247,15 @@ async fn api_owner(data: &str) -> String {
let result = match resp { let result = match resp {
Ok(value) => value, Ok(value) => value,
Err(_err) => return "{\"error\":\"rpc call failed\"}".to_string(), Err(_err) => return Json("{\"error\":\"rpc call failed\"}".to_string()),
}; };
return result.to_string(); return Json(result.to_string());
} }
"{\"error\":\"not allowed\"}".to_string() Json("{\"error\":\"not allowed\"}".to_string())
} else { } else {
"{\"error\":\"not allowed\"}".to_string() Json("{\"error\":\"not allowed\"}".to_string())
} }
} }
@@ -264,30 +263,30 @@ async fn api_owner(data: &str) -> String {
// Foreign API. // Foreign API.
// All methods are whitelisted. // All methods are whitelisted.
#[post("/v2/foreign", data="<data>")] #[post("/v2/foreign", data="<data>")]
async fn api_foreign(data: &str) -> String { async fn api_foreign(data: &str) -> Json<String> {
if CONFIG.public_api == "enabled" { if CONFIG.public_api == "enabled" {
let result = serde_json::from_str(data); let result = serde_json::from_str(data);
let v: Value = match result { let v: Value = match result {
Ok(value) => value, Ok(value) => value,
Err(_err) => return "{\"error\":\"bad syntax\"}".to_string(), Err(_err) => return Json("{\"error\":\"bad syntax\"}".to_string()),
}; };
let method = match v["method"].as_str() { let method = match v["method"].as_str() {
Some(value) => value, Some(value) => value,
_ => return "{\"error\":\"bad syntax\"}".to_string(), _ => return Json("{\"error\":\"bad syntax\"}".to_string()),
}; };
let resp = requests::call(method, v["params"].to_string().as_str(), v["id"].to_string().as_str(), "foreign").await; let resp = requests::call(method, v["params"].to_string().as_str(), v["id"].to_string().as_str(), "foreign").await;
let result = match resp { let result = match resp {
Ok(value) => value, Ok(value) => value,
Err(_err) => return "{\"error\":\"rpc call failed\"}".to_string(), Err(_err) => return Json("{\"error\":\"rpc call failed\"}".to_string()),
}; };
return result.to_string(); return Json(result.to_string());
} else { } else {
"{\"error\":\"not allowed\"}".to_string() Json("{\"error\":\"not allowed\"}".to_string())
} }
} }

View File

@@ -80,9 +80,9 @@ pub async fn call(method: &str, params: &str, id: &str, rpc_type: &str) -> Resul
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let result = client.post(rpc_url) let result = client.post(rpc_url)
.body(format!("{{\"method\": \"{}\", \"params\": {}, \"id\": {}}}", method, params, id)) .body(format!("{{\"method\": \"{}\", \"params\": {}, \"id\": {}, \"jsonrpc\": \"2.0\"}}", method, params, id))
.basic_auth(CONFIG.user.clone(), Some(secret)) .basic_auth(CONFIG.user.clone(), Some(secret))
.header("content-type", "plain/text") .header("content-type", "application/json")
.send() .send()
.await?; .await?;