Skip to content

Commit 4fd0085

Browse files
author
Jim Hodapp
authored
Merge pull request #2 from Jim-Hodapp-Coaching/fix/#1
Fix/InvalidJSONPOST/#1
2 parents 1735eab + 6eb4891 commit 4fd0085

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/main.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use rand::{thread_rng, Rng};
22
use reqwest::blocking::Client;
3+
use reqwest::header::CONTENT_TYPE;
34
use serde::{Serialize, Deserialize};
45
use std::fmt;
56

67
#[derive(Serialize, Deserialize)]
78
struct Reading {
8-
tempurature: String,
9+
temperature: String,
910
humidity: String,
1011
pressure: String,
1112
dust_concentration: String,
@@ -14,18 +15,18 @@ struct Reading {
1415

1516
impl Reading {
1617
fn new(
17-
tempurature: String,
18+
temperature: String,
1819
humidity: String,
1920
pressure: String,
2021
dust_concentration: String,
2122
air_purity: String
2223
) -> Reading {
2324
Reading {
24-
tempurature: tempurature,
25-
humidity: humidity,
26-
pressure: pressure,
27-
dust_concentration: dust_concentration,
28-
air_purity: air_purity
25+
temperature,
26+
humidity,
27+
pressure,
28+
dust_concentration,
29+
air_purity
2930
}
3031
}
3132
}
@@ -68,7 +69,7 @@ fn random_gen_humidity() -> String {
6869
format!("{:.1}", value)
6970
}
7071

71-
fn random_gen_tempurature() -> String {
72+
fn random_gen_temperature() -> String {
7273
let mut rng = thread_rng();
7374
let value = rng.gen_range(15.0..=35.0);
7475
format!("{:.1}", value)
@@ -88,7 +89,7 @@ fn main() {
8889
let dust_concentration = random_gen_dust_concentration();
8990
let air_purity = AirPurity::from_value(dust_concentration.parse::<u16>().unwrap()).to_string();
9091
let reading = Reading::new(
91-
random_gen_tempurature(),
92+
random_gen_temperature(),
9293
random_gen_humidity(),
9394
random_gen_pressure(),
9495
dust_concentration,
@@ -101,7 +102,9 @@ fn main() {
101102
println!("Sending POST request to {} as JSON: {}", URL, json);
102103

103104
let client = Client::new();
104-
let res = client.post(URL)
105+
let res = client
106+
.post(URL)
107+
.header(CONTENT_TYPE, "application/json")
105108
.body(json)
106109
.send();
107110

@@ -122,8 +125,8 @@ mod tests {
122125
}
123126

124127
#[test]
125-
fn random_gen_tempurature_returns_correctly_formatted_humidity_data() {
126-
let result = random_gen_tempurature();
128+
fn random_gen_temperature_returns_correctly_formatted_humidity_data() {
129+
let result = random_gen_temperature();
127130
let regex = Regex::new(r"\d{1,2}.\d{1}").unwrap();
128131

129132
assert!(regex.is_match(&result));

0 commit comments

Comments
 (0)