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

POST always has content_type 'application/x-www-form-urlencoded'? #104

Closed
olliebun opened this issue Dec 5, 2011 · 11 comments
Closed

POST always has content_type 'application/x-www-form-urlencoded'? #104

olliebun opened this issue Dec 5, 2011 · 11 comments
Assignees
Milestone

Comments

@olliebun
Copy link

olliebun commented Dec 5, 2011

I'm using Rest-Client to submit a simple POST to CouchDB, with content-type application/json. Rest-Client's UrlEncoded class has this code, though:

def headers
  super.merge({'Content-Type' => 'application/x-www-form-urlencoded'})
end

Is there a way around this? I don't see why the content type should be messed with if the developer has specified it.

I'm calling it like this:

RestClient.post uri, payload, headers, &block

Here are the parameters:

URI: http://cerales:password@localhost:5984/testing
Payload: {"dan":true,"yep":1}
Headers: {:content_type=>:json, :accept=>:json}

Payload is a string, but I've tried it with a hash too.

@lpar
Copy link

lpar commented Dec 12, 2011

This seems to work for me:

RestClient.post url, { 'body' => msg }.to_json, :content_type => 'application/json'

In the Sinatra app:

post '/', :provides => :json do
  logger.info "Media type is " + request.media_type

Result:

I, [2011-12-12T16:02:29.201537 #22150]  INFO -- : Media type is application/json

@quigebo
Copy link

quigebo commented Jul 31, 2012

@Cerales - try adding a #to_json to your payload

@mhenrixon
Copy link

I have the same issue, rest-client is broken so time to turn to something else I guess. Sucks because the API is nice but I need to be able to post json and it won't let me.

RestClient.post "https://#{uid}:#{pwd}@api.twilio.com/#{uri}", { "to" => to, "from" => from, "body" => "Hey ho let's go" }.to_json, { content_type: 'application/json' }

that request tells me the content-type is wrong and that I should specificy 'application/x-www-form-urlencoded' instead so obviously this needs to be fixed.

@jcoyne
Copy link
Contributor

jcoyne commented Nov 1, 2013

It's working fine for me. Can't duplicate.

@ab
Copy link
Member

ab commented Mar 28, 2014

Sorry, I wasn't able to reproduce this on master. Are you still seeing this? Could you provide a test case?

@matkruse
Copy link

The problem seems to be that if the payload is being passed in as a hash, then the content-type is overridden. Unless you want to pass a header overridden message back to the user due to the payload structure, I think you can close this issue. @quigebo is right on.

@ab
Copy link
Member

ab commented Nov 12, 2015

Yeah, it seems possible that rest-client should raise an exception if you pass a Content-Type header and also a Hash for the payload, since that combination is incompatible. Also possible it would be less intrusive to simply warn().

@ab ab self-assigned this Nov 12, 2015
@ryannmicua
Copy link

Would be nice if this was at least in the examples.

One wouldn't expect to have to convert a hash to a json string when all the other examples show the payload being a hash. Also, one would expect to be able to "override" the content type (and other headers) and not be overridden without the user knowing about it.

@cazador4
Copy link

+1

@ioquatix
Copy link

Yeah, it seems possible that rest-client should raise an exception if you pass a Content-Type header and also a Hash for the payload, since that combination is incompatible. Also possible it would be less intrusive to simply warn().

Why not derive the encoding of the hash from the content_type specified?

sobrinho added a commit to sobrinho/shippo-ruby-client that referenced this issue Aug 31, 2017
Using a GET request we must not send the payload, otherwise RestClient
will complain because isn't allowed to use content type as JSON and
payload as a Hash.

See rest-client/rest-client#104
oehlschl pushed a commit to goatapp/shippo-ruby-client that referenced this issue Sep 6, 2017
Using a GET request we must not send the payload, otherwise RestClient
will complain because isn't allowed to use content type as JSON and
payload as a Hash.

See rest-client/rest-client#104
tlloydthwaites added a commit to University-of-Divinity/keycloak-admin-ruby that referenced this issue Oct 12, 2022
Using a raw hash instead of JSON payload was causing a header override warning in rest-client.

See rest-client/rest-client#104 for details; because an empty hash is passed directly as the payload, rest-client complains about incompatible headers. Solution is to simply json encode the empty hash.
@YOHN89
Copy link

YOHN89 commented Nov 17, 2022

+1

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

No branches or pull requests