mamantoha / crest

HTTP and REST client for Crystal
https://mamantoha.github.io/crest/
MIT License
235 stars 14 forks source link

How to post more complex JSON data #157

Closed richardboehme closed 3 years ago

richardboehme commented 3 years ago

Hey! 👋 Thanks for this great shard! I'm currently using it to consume an API that requires a JSON schema, that contains arrays and numbers. If I understand correctly one can use params that are converted to JSON automatically. However params have the type Hash(String, String) so we cannot use different types for values.

According to the documentation there is the possibility to pass a raw string as a second argument to Crest.post. However, this throws a compiler error for me:

Error: no overload matches 'Crest.post' with types String, String

Was this behavior changed recently and is there an alternative to use?

mamantoha commented 3 years ago

Hi.

The latest release (v0.27.1) does not speak JSON natively, so convert your hash to a string with .to_json method before passing it to crest.

Crest.post(
  "http://httpbin.org/post",
  form: {"foo" => "bar"}.to_json
  headers: {"Accept" => "application/json"},
)

The future version will do the same with:

Crest.post("http://httpbin.org/post", {"foo" => "bar"}, json: true)

You can use the latest version from this repository by changing your shards.yml

dependencies:
  crest:
    github: mamantoha/crest
    branch: master
richardboehme commented 3 years ago

Ah this is great, thank you! I was looking at the README of the master branch... Sorry!