# Faraday using \`get\` for PUT/DELETE requests

**URL:** https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451
**Category:** Support
**Created:** [June 8, 2016, 10:28pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451 "2016-06-08T22:28:44Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![PureSin](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@PureSin](https://meta.discourse.org/u/PureSin)
#### Post date: [June 8, 2016, 10:28pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/1 "2016-06-08T22:28:44Z")

</div>

Hi,

We have a hosted discuss at [http://discuss.codecademy.com](http://discuss.codecademy.com). We have a few groups that we add users to based on their status on our site. A few weeks back we noticed users weren’t being added these additional groups.

So I tried using the api to add a test user to a group:

```plaintext
@client = DiscourseApi::Client.new(config['api_url'])
@client.api_key = config['api_key']
@client.api_username = config['api_username']

@client.user('kelvin14') # => {"id"=>7..... my user
@client.groups # {"id"=>51, "automatic"=>false, "name"=>"test_group", ... the group

@client.group_add(51, {usernames: ['kelvin14']}) 

```

the returned response is:

```plaintext
#<Faraday::Env @method=:get @body="" @url=#<URI::HTTPS https://discuss.codecademy.com/admin/groups/51/members.json?api_key=....> @request=#<Faraday::RequestOptions (empty)> @request_headers={"Accept"=>"application/json", "User-Agent"=>"DiscourseAPI Ruby Gem 0.10.1", "Content-Type"=>"application/x-www-form-urlencoded"} @ssl=#<Faraday::SSLOptions verify=true> @response=#<Faraday::Response:0x007ff975e667b8 @on_complete_callbacks=[], @env=#<Faraday::Env @method=:get @body="" @url=#<URI::HTTPS https://discuss.codecademy.com/admin/groups/51/members.json?api_key=...... "strict-transport-security"=>"max-age=31415926"} @status=200>

```

Note the method is :get. Even though in discourse\_api’s groups.rb the request is `put("/admin/groups/#{group_id}/members.json", users)` I see this happening with all requests are using get (deleting groups, etc)

Eventually I tried removing `conn.use FaradayMiddleware::FollowRedirects, limit: 5 ` in client.rb which I see the requests actually using `:put` but status is 301 instead.

Any idea on why this is happening?

Thanks

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [June 8, 2016, 10:47pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/2 "2016-06-08T22:47:34Z")

</div>

Please delete that API Key from your live/staging site and regen it. Google has likely picked it up by now.

I’ll see if anything else stands out with your api request later tonight/tomorrow

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [June 9, 2016, 12:16pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/3 "2016-06-09T12:16:29Z")

</div>

Is this an issue with the API you are using? As the Discourse Web App doesn’t submit a request to `/admin/groups/51/members.json`, instead it submits the request to `/groups/51/members.json`

I see the Discourse API repo references /admin/groups/, but that definitely isn’t being used by the Discourse App

> <https://github.com/discourse/discourse_api/blob/main/lib/discourse_api/api/groups.rb#L17-L32>

From the /groups/special page as a Group Owner/Admin

 ![](https://global.discourse-cdn.com/meta/original/3X/7/4/74b0fb835797f01a79ecd245efc315bbbaca1aed.png)

From the /admin/groups/custom/Special page as an Admin

 ![](https://global.discourse-cdn.com/meta/original/3X/f/c/fca1a634eef8855daa4c6168c956bd22dec6bb87.png)

Both used the following relevant Request Headers  
 ![](https://global.discourse-cdn.com/meta/original/3X/2/0/20a833f01e290983c65cf5abddcd9213635938d2.png)

---

<div class="post-metadata">

### Author: ![PureSin](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@PureSin](https://meta.discourse.org/u/PureSin)
#### Post date: [June 9, 2016, 1:36pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/4 "2016-06-09T13:36:22Z")

</div>

Yes I noticed the same thing and took a look at the discourse repo, the `/admin/groups` are aliased to `/groups`

In routes.rb line 388

```plaintext
# aliases so old API code works
  delete "admin/groups/:id/members" => "groups#remove_member", constraints: AdminConstraint.new
  put "admin/groups/:id/members" => "groups#add_members", constraints: AdminConstraint.new

```

And that’s whats on the master branch of discourse\_api: [discourse\_api/lib/discourse\_api/api/groups.rb at ba8a75d088f2b5ca94c31a1daed17fcbf1491e50 · discourse/discourse\_api · GitHub](https://github.com/discourse/discourse_api/blob/ba8a75d088f2b5ca94c31a1daed17fcbf1491e50/lib/discourse_api/api/groups.rb#L31)

I tried removing the `admin` and making the call but got `InvalidAccess` error

---

<div class="post-metadata">

### Author: ![PureSin](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@PureSin](https://meta.discourse.org/u/PureSin)
#### Post date: [June 10, 2016, 8:57pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/5 "2016-06-10T20:57:50Z")

</div>

hi @cpradio any update on this issue? Thanks.

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [June 10, 2016, 10:44pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/6 "2016-06-10T22:44:28Z")

</div>

Unfortunately no further progress so far. I can’t find anything indicating an issue and I haven’t had time to try and create an app to add a user to a group.

---

<div class="post-metadata">

### Author: ![PureSin](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@PureSin](https://meta.discourse.org/u/PureSin)
#### Post date: [June 13, 2016, 2:08pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/7 "2016-06-13T14:08:36Z")

</div>

Is it possible for me to get an api for [http://try.discourse.org/](http://try.discourse.org/) to see if I can replicate the issue there?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [June 13, 2016, 2:13pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/8 "2016-06-13T14:13:15Z")

</div>

The code below works for me

```plaintext
conn = Faraday.new(:url => 'https://forum.com.br/') do |faraday|
  faraday.request :url_encoded # form-encode POST params
  faraday.response :logger # log requests to STDOUT
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
  faraday.options.params_encoder = Faraday::FlatParamsEncoder
end

payload = { api_key: 'asdasdasdasdasdasdasdasdasd', api_username: 'qweqweqwe' }

payload[:group_id] = 55

payload['users[]'] = ['asdasdasdasd', 'gfghghf']

conn.put '/admin/groups/bulk', payload

```

---

<div class="post-metadata">

### Author: ![PureSin](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@PureSin](https://meta.discourse.org/u/PureSin)
#### Post date: [June 13, 2016, 2:59pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/9 "2016-06-13T14:59:50Z")

</div>

@Falco can you try the request again with the following options for faraday (copied from the discourse\_api gem)

```plaintext
# Follow redirects
        faraday.use FaradayMiddleware::FollowRedirects, limit: 5 
        # Convert request params to "www-form-encoded"
        faraday.request :url_encoded
        # Parse responses as JSON
        faraday.use FaradayMiddleware::ParseJson, content_type: 'application/json'
        # Use Faraday's default HTTP adapter
        faraday.adapter :net_http

```

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [June 22, 2016, 10:34pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/10 "2016-06-22T22:34:48Z")

</div>

Did you get this to work, finally?

---

<div class="post-metadata">

### Author: ![PureSin](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@PureSin](https://meta.discourse.org/u/PureSin)
#### Post date: [June 24, 2016, 6:28pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/11 "2016-06-24T18:28:23Z")

</div>

Yes, the cause was our config still used http version of the url instead of the https version. Caused Faraday to only work on Get requests for put/delete/post will get redirected.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [June 24, 2016, 6:43pm UTC](https://meta.discourse.org/t/faraday-using-get-for-put-delete-requests/45451/12 "2016-06-24T18:43:55Z")

</div>


