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

How send POST request with form data? #56

Closed
tit opened this issue Jun 23, 2015 · 4 comments
Closed

How send POST request with form data? #56

tit opened this issue Jun 23, 2015 · 4 comments

Comments

@tit
Copy link
Contributor

tit commented Jun 23, 2015

Hello. I want send POST request with form data.
All my requests don't send request with :form.
Why?

# encoding: utf-8
require 'airborne'
describe '' do
  it '' do
    post 'http://httpbin.org/post', {}, {params: {foo: 'bar'}}    
    puts json_body # {:args=>{:foo=>"bar"}, :data=>"{}", :files=>{}, :form=>{}, :headers=>{:Accept=>"*/*; q=0.5, application/xml", :"Accept-Encoding"=>"gzip, deflate", :"Content-Length"=>"2", :"Content-Type"=>"application/json", :Host=>"httpbin.org", :"User-Agent"=>"Ruby"}, :json=>{}, :origin=>"195.151.220.177", :url=>"http://httpbin.org/post?foo=bar"}
    post 'http://httpbin.org/post', {foo: 'bar'}
    puts json_body # {:args=>{}, :data=>"{\"foo\":\"bar\"}", :files=>{}, :form=>{}, :headers=>{:Accept=>"*/*; q=0.5, application/xml", :"Accept-Encoding"=>"gzip, deflate", :"Content-Length"=>"13", :"Content-Type"=>"application/json", :Host=>"httpbin.org", :"User-Agent"=>"Ruby"}, :json=>{:foo=>"bar"}, :origin=>"195.151.220.177", :url=>"http://httpbin.org/post"}
  end
end

Soooooory for my bad english. :(

@brooklynDev
Copy link
Owner

So I'm not sure exactly what the issue you're having is, I apologize in advance if I'm misunderstanding.

When you run this curl command:

curl -H "Content-Type: application/json" -X POST -d '{"foo":"bar"}' http://httpbin.org/post

You'll notice that you get back the following data from httpbin.org:

{
  "args": {},
  "data": "{\"foo\":\"bar\"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "13",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.37.1"
  },
  "json": {
    "foo": "bar"
  },
  "url": "http://httpbin.org/post"
}

When you make the same request using airborne:

post 'http://httpbin.org/post', {foo: 'bar'}

Here's the json_body:

{:args=>{}, :data=>"{\"foo\":\"bar\"}", :files=>{}, :form=>{}, :headers=>{:Accept=>"*/*; q=0.5, application/xml", :"Accept-Encoding"=>"gzip, deflate", :"Content-Length"=>"13", :"Content-Type"=>"application/json", :Host=>"httpbin.org", :"User-Agent"=>"Ruby"}, :json=>{:foo=>"bar"}, :url=>"http://httpbin.org/post"}

which does appear to be the same. I'm not sure what the form property for httpbin.org is supposed to return, but I don't think there's an issue here with airborne.

I'm going to close this issue for now, feel free to close if I misunderstood and you're still having issues.

@Paxa
Copy link

Paxa commented Dec 13, 2015

Having a same problem. The issue is how to submit data encoded as application/x-www-form-urlencoded instead of application/json.

If we follow example from README and send like this:

post 'http://httpbin.org/post', {}, {params: {foo: 'bar'}}

Result will be:

{
  "args": {
    "foo": "bar"
  }, 
  "data": "{}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*; q=0.5, application/xml", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "2", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "Ruby"
  }, 
  "json": {}, 
  "origin": "202.62.16.254", 
  "url": "http://httpbin.org/post?foo=bar"
}

As I see option params add data to query string, but how to add data in request body? Same as when we submit form with method="post"


After going through source code I found solution to send form data in body:

post('http://httpbin.org/post', {foo: 'bar'}.to_query, 'Content-Type' => 'application/x-www-form-urlencoded')

I think there should be a proper documentation for DSL methods with information about attributes and possible types

@henryaj
Copy link

henryaj commented Feb 21, 2016

I have the same problem. Is there a fix in sight, or do we have to use @Paxa's workaround for the time being?

@jdimmerman
Copy link

@brooklynDev @Paxa Were you ever able to come to a consensus as to whether this is an issue? I'm running into the same thing - unable to figure out how to encode the body as application/x-www-form-urlencoded

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

5 participants