1

Too my knowledge there are no guides on how to write HTTP POST requests to a JSON-RPC API with http utilities that are available to Substrate offchain workers. What I understand currently is that the body has to be a buffered stream of sorts (or so it seems). But say I have a JSON body:

let body = r#"{"jsonrpc": "2.0", "method": "someMethod" ... }"#

I have attempted to take the Request struct and construct a POST request like so:

let body = ...snip...

let my_request = sp_runtime::offchain::http::Request::post("http:://some-url/", body.as_bytes()); 

I have played around with trying to use serde_json::json macro to construct a serde_json::Value then take value.to_vec() (a few other steps to this that I am omitting).

All in all does anyone have a good example of how I can send a simple POST request to a JSON-RPC endpoint, and how to properly add the request body?

1 Answer 1

0

To inform the server that you are sending a JSON request, simply include the following header:

Content-Type: application/json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.