Created
August 23, 2024 10:05
-
-
Save mvidner/0e22ea82abecb44b469841929e5ec279 to your computer and use it in GitHub Desktop.
Parse httpmock body from numbers to text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# The Rust httpmock crate, | |
# https://docs.rs/httpmock/latest/httpmock/ | |
# in the 0.7.0 version, when it logs the request, it shows the body as | |
# a numeric array of bytes. | |
# Make it readable. | |
# Input: | |
# [2024-08-23T08:22:33Z DEBUG httpmock::server::web::handlers] Could not match any mock to the following request: HttpMockRequest { | |
# path: "/api/software/config", | |
# method: "PUT", | |
# headers: Some(...) | |
# body: Some( | |
# [ | |
# 123, | |
# 34, | |
# 112, | |
# ... | |
# 125, | |
# ], | |
# ), | |
text = ARGF.read | |
m = text.match(/body: Some\(\s*(\[.*\]),/m) | |
# "[123,34,112,34,58,123,34,120,102,99,101,34,58,116,114,117,101,125,125]" | |
numeric_array_source = m[1].gsub(/\s/, "").sub(",]", "]") | |
# as if we eval'd, but safe | |
# [123, 34, 112, 34, 58, 123, 34, 120, 102, 99, 101, 34, 58, 116, 114, 117, 101, 125, 125] | |
parsed = numeric_array_source.tr("][", "").split(",").map { |s| s.to_i } | |
body = parsed.map(&:chr).join("") | |
# {"p":{"xfce":true}} | |
puts body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment