Kong / unirest-ruby

Unirest in Ruby: Simplified, lightweight HTTP client library.
http://unirest.io/ruby
MIT License
364 stars 81 forks source link

How to post json? #13

Closed zacker330 closed 9 years ago

zacker330 commented 10 years ago

My service is not supporting parameters, just recieve json entity. So I wanna to post json entity to server, how can I do that?

thanks.

corprew commented 10 years ago

@zacker330 check out the "Custom Entity Body" example on http://unirest.io/ruby.html

zacker330 commented 10 years ago

@corprew cool. I missing the document. thanks.

jikkujose commented 8 years ago

Somehow I am unable to do a post with JSON, this is what I did:

response = Unirest.post(
  URL,
  headers: { "Accept" => "application/json" },
  parameters: {keyInJSON: 'valueReceived'}.to_json
)

p response

Am I missing something? The service is throwing error saying missing key, but I am proving everything right. I checked the data with a command line & it works!

chrise86 commented 6 years ago

I'm also having this problem:

def publish
  Unirest.post ENV.fetch('ENDPOINT'),
               headers: headers,
               parameters: parameters
end

private

def parameters
  {
    type: @@event_type,
    data: @data.deep_transform_keys! { |key| key.to_s.camelize(:lower) }
  }.to_json
end

def headers
  headers = {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json' # tried with and without this
  }
  headers['Authorization'] = bearer_token if @user_id.present?
  headers
end