Comments (7)
Based on the curl request example in Moment's docs, you need to post JSON data. So switch builder.request :url_encoded
to builder.request :json
and it'll work (I just tested this).
from faraday.
You never set a Faraday::Request
type on your connection. You probably want to add either builder.use Faraday::Request::UrlEncoded
or Faraday::Request::JSON
(if you want to post JSON data).
from faraday.
Well, I added builder.use Faraday::Request::UrlEncoded
and that error is gone but I'm not sure why I am getting a 422 response
back.
Let me make it more obvious so that I can reveal the problem much clearer.
I am wrapping a API call to a service called Moment via its api http://momentapp.com/docs
So, here is the code just using the Faraday only, without any third party stack.
And the api key I've pasted over here is the real one of mine. You can run it and it will make a real request with that valid key of mine, so that it can be debugged.
require 'faraday'
conn = Faraday.new('https://momentapp.com', ssl: {verify: false}) do |builder|
builder.request :url_encoded
builder.response :logger
builder.adapter :net_http
end
resp = conn.post '/jobs.json', {:apikey => "kOrROwbVPNC34VZYhbET", :job => {:method => "PUT", :at => '2012-01-31T18:36:21', :uri => "http://kasko.com"}}
puts resp.body
and the response is:
I, [2011-07-16T23:52:16.403878 #3004] INFO -- : post https://momentapp.com/jobs.json
D, [2011-07-16T23:52:16.404004 #3004] DEBUG -- request: Content-Type: "application/x-www-form-urlencoded"
I, [2011-07-16T23:52:18.626578 #3004] INFO -- Status: 422
D, [2011-07-16T23:52:18.626754 #3004] DEBUG -- response: server: "nginx/0.7.67"
date: "Sat, 16 Jul 2011 18:07:18 GMT"
content-type: "text/html"
connection: "close"
content-length: "711"
x-runtime: "0.086304"
<!DOCTYPE html>
<html>
<head>
<title>The change you wanted was rejected (422)</title>
<style type="text/css">
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
div.dialog {
width: 25em;
padding: 0 4em;
margin: 4em auto 0 auto;
border: 1px solid #ccc;
border-right-color: #999;
border-bottom-color: #999;
}
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
</style>
</head>
<body>
<!-- This file lives in public/422.html -->
<div class="dialog">
<h1>The change you wanted was rejected.</h1>
<p>Maybe you tried to change something you didn't have access to.</p>
</div>
</body>
</html>
What might be wrong coz this same request with same parameters and api calls with HTTParty gets the response successfully, but not with Faraday?
from faraday.
Yup! it works.
But I read that documentation couple of times and I didn't see anything about the request type to be in json.
It only says that it should be escaped. It only says about the response is in json. Did I miss any line on reading the doc?
The first job posting says this format.
POST https://momentapp.com/jobs.json?
job[at]=2011-01-31T18:36:21&
job[method]=POST&
job[uri]=http://yourapp.com/?var1=true&var2=false&
apikey=[your_api_key]
But still to be clear, does that url ending in .json
is supposed be saying that the request should be json format?
Thanks, coz it works!
from faraday.
And the .json
in the URL means that the response will be in JSON, not that your request is.
from faraday.
It's not super clear, but if you look at the example using curl
, they do in fact specify the Content-Type as application/json
. They probably should be clearer about that.
I think the escaping/encoding only applies if you put your data inline in the URL (/jobs.json?apikey=123&job[at]=...), rather than a post body.
from faraday.
Thanks once again for the explanation.
from faraday.
Related Issues (20)
- Block private ips (allow_private_ips) HOT 3
- Multiple "already initialized constant" warnings HOT 2
- NoMethodError: undefined method `method=' for #<Faraday::RequestOptions (empty) HOT 5
- Faraday::Response - argument order causes unexpected behaviour HOT 3
- Incompatible with Firebase-admin-sdk HOT 1
- Streaming Stop with Connection Close? HOT 3
- `Faraday::Request::Json` doesn't encode the value `false` while it encodes `true` to `"true"` HOT 2
- Lots of "warning: method redefined; discarding old" printed during testing HOT 7
- Faraday using PUT with Transfer-Encoding: chunked
- Faraday::ParsingError.response_status doesn't return expected value when stubbing response HOT 5
- Missing HTTP 408 Request Timeout specific Faraday ClientError? HOT 3
- Dead link to Faraday Team in README and docs HOT 1
- Strange behaviour with response HOT 1
- Setting ActiveRecord DatabaseResolver to enable multi database does not work with Faraday adapter HOT 3
- Unable to use webdav verbs HOT 3
- Change connection proxy HOT 2
- Drop Ruby < 3.0 support HOT 1
- `no_proxy` Parameter in Manual Proxy Configuration HOT 4
- connection headers are ignored when using build_response directly HOT 5
- Add tests and documentation to make sure adapters support streaming requests HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from faraday.