Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for POST request with custom headers #317

Open
Eyal-Shalev opened this issue May 17, 2018 · 1 comment
Open

Add examples for POST request with custom headers #317

Eyal-Shalev opened this issue May 17, 2018 · 1 comment

Comments

@Eyal-Shalev
Copy link

No description provided.

@Eyal-Shalev Eyal-Shalev changed the title Add examples for using POST request with custom headers Add examples for POST request with custom headers May 17, 2018
@nic-lan
Copy link

nic-lan commented Aug 7, 2018

In my case i wanted to do a post request with a json body but i was experiencing some issues becase the HttpClient was handling the body as it would have been a form input.
I fixed the issue with by overriding ( in my code ) the behavior for the HttpClient#form_encode_body.

module EventMachine
  class HttpClient
    # We need to override the bohavior of the gem class, where a hash is considered a form
    # and not flexibility is given to deal with json body.
    # Please visit https://github.com/igrigorik/em-http-request
    # The issue is at https://github.com/igrigorik/em-http-request/blob/master/lib/em-http/http_encoding.rb#L89
    def form_encode_body(body)
      body.to_json
    end
  end
end

Also i set the headers for application/json in the adapter where the request is done.

class Adapter
    HEADERS = {
      'content-type' => 'application/json',
      'accept' => 'application/json'
    }

  def send_request(post_url, body)
    ....
    conn = EventMachine::HttpRequest.new(post_url)
    conn.use EventMachine::Middleware::JSONResponse

    response = conn.post(head: HEADERS, body: body)
    ...
  end
end

Maybe we would like to make the gem able to deal with this case scenario ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants