Code Monkey home page Code Monkey logo

Comments (7)

dmarkow avatar dmarkow commented on August 15, 2024 1

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.

dmarkow avatar dmarkow commented on August 15, 2024

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.

millisami avatar millisami commented on August 15, 2024

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.

millisami avatar millisami commented on August 15, 2024

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.

dmarkow avatar dmarkow commented on August 15, 2024

And the .json in the URL means that the response will be in JSON, not that your request is.

from faraday.

dmarkow avatar dmarkow commented on August 15, 2024

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.

millisami avatar millisami commented on August 15, 2024

Thanks once again for the explanation.

from faraday.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.