Reputation: 277
I am creating a Rust web application. I am trying to make API requests and pass a result from a request as Response to the web view. There are main.rs , route.rs and common.rs files. Basically the main.rs file calls the relevant route and then the route will call the function. The thing is, now there are no errors when I build it. But, when I tried to run it using the web browser, it gives me this error in the browser.
This page isn’t working
127.0.0.1 didn’t send any data.
ERR_EMPTY_RESPONSE
And also this shows up in the terminal:
thread 'actix-rt:worker:2' panicked at 'assertion failed: !headers.contains_key(CONTENT_TYPE)', src\search\routes\common.rs:15:9
What I want to know is, am I sending headers in correct way or not? Or am I doing anything wrong? How can I fix this?
route.rs
use crate::search::User;
use actix_web::{get, post, put, delete, web, HttpResponse, Responder};
use serde_json::json;
extern crate reqwest;
extern crate serde;
mod bfm;
mod common;
use actix_web::Result;
use actix_web::error;
use actix_web::http::{StatusCode};
#[get("/token")]
async fn get_token() -> Result<String> {
let set_token = common::authenticate();
return set_token.await
.map_err(|_err| error::InternalError::new(
std::io::Error::new(std::io::ErrorKind::Other, "failed to get token"),
StatusCode::INTERNAL_SERVER_ERROR
).into());
}
pub fn init_routes(cfg: &mut web::ServiceConfig) {
cfg.service(get_token);
}
common.rs
extern crate reqwest;
use reqwest::header::HeaderMap;
use reqwest::header::AUTHORIZATION;
use reqwest::header::CONTENT_TYPE;
pub async fn authenticate() -> Result<String, reqwest::Error> {
fn construct_headers() -> HeaderMap {
let mut headers = HeaderMap::new();
headers.insert(AUTHORIZATION, "Basic bghtyujhu==".parse().unwrap());
headers.insert(CONTENT_TYPE, "application/x-www-form-urlencoded".parse().unwrap());
assert!(headers.contains_key(AUTHORIZATION));
assert!(!headers.contains_key(CONTENT_TYPE));
headers
}
let client = reqwest::Client::new();
let reszr = client.post("https://api.test.com/auth/token")
.headers(construct_headers())
.body("grant_type=client_credentials")
.send()
.await?
.text()
.await;
return reszr;
}
Upvotes: 1
Views: 1194