Serde_json error in posting file to remote server

I am using following code to post a json file to an http server (https://paste.rs/). Everything goes normal but when the json file is posted there, the contents of json file are store in reverse order. I am giving the code, the actual json file contents and the json file posted to http server.

The code used to post a Json file to http server

use error_chain::error_chain;
use serde_json:: {Value};
use std::env;
use std::fs;

error_chain! {
     foreign_links {
         HttpRequest(reqwest::Error);
         IoError(::std::io::Error);
     }
 }
 #[tokio::main]

async fn main() -> Result<()> {
    //Get file name from command line argument
    let args: Vec<String> = env::args().collect();    
    let file_name = &args[1];
    println!("The file name is: {}", file_name);

    //First load the file into a string
    let text = fs::read_to_string(file_name)?;
    println!("The text of the file is {}", text);

    //Parse the string into a dynamically-typed JSON structure.
    let json_file= serde_json::from_str::<Value>(&text).unwrap();
    

    //Post the file to an http server     
    let client = reqwest::Client::new();
    let res = client.post("https://paste.rs")
        .json(&json_file)
        .send()
        .await?;

    //Get and print responce values
     let response_text = res.text().await?;
     println!("Your paste is located at: {}",response_text );   
     Ok(())            
}

Contents of actual Json File

{
    "quiz": {
        "sport": {
            "q1": {
                "question": "Which one is correct team name in NBA?",
                "options": [
                    "New York Bulls",
                    "Los Angeles Kings",
                    "Golden State Warriros",
                    "Huston Rocket"
                ],
                "answer": "Huston Rocket"
            }
        },
        "maths": {
            "q1": {
                "question": "5 + 7 = ?",
                "options": [
                    "10",
                    "11",
                    "12",
                    "13"
                ],
                "answer": "12"
            },
            "q2": {
                "question": "12 - 8 = ?",
                "options": [
                    "1",
                    "2",
                    "3",
                    "4"
                ],
                "answer": "4"
            }
        }
    }
}

The contents of file posted to http server

{
    "quiz": {
        "maths": {
            "q1": {
                "answer": "12",
                "options": [
                    "10",
                    "11",
                    "12",
                    "13"
                ],
                "question": "5 + 7 = ?"
            },
            "q2": {
                "answer": "4",
                "options": [
                    "1",
                    "2",
                    "3",
                    "4"
                ],
                "question": "12 - 8 = ?"
            }
        },
        "sport": {
            "q1": {
                "answer": "Huston Rocket",
                "options": [
                    "New York Bulls",
                    "Los Angeles Kings",
                    "Golden State Warriros",
                    "Huston Rocket"
                ],
                "question": "Which one is correct team name in NBA?"
            }
        }
    }
}

JSON maps are unordered, you shouldn't rely on any parsing or serializing library preserving the order unless they explicitly specify that they do so[1] .

If you're just sending the JSON data there's not really any reason to parse it though. You can just set the body data directly and manually add the content type header.


  1. and generally caring about the order of keys in a JSON object is just asking for trouble since that's not how the standard works ↩ī¸Ž

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.