Search code examples
ajaxhttpsemantics

What HTTP verb should be used for an AJAX request that POSTs to an external API?


Consider the following scenario: As an after-effect of a user signing up for your site, you want to POST to an external API (say, an email service provider) documenting the fact that that user signed up.

This request is performed asynchronously as an AJAX request from the client to your application. This request in turn fires a POST request to the external API. The AJAX request has no further effects, and in particular it does not change the state of your application's database.

My question is: what is the proper HTTP verb for the AJAX request and why? I've consulted the HTTP spec, but can't extract an answer from it.

Here's some pseudo Rails code representing a controller action that would have the properties described above:

class UsersController < ApplicationController

  def ajax_esp_signup
    email = User.find(params[:id]).email
    response = EspApi.post_signup(email) # Send POST request to external API.
    success = response['error'].nil?

    render :json => {
      :success => success
    }
  end

end

Solution

  • POST. The important thing here is the request has a side effect, not whether it has an effect on your database in particular. Since the external API request is a POST, we assume it's non-idempotent, so you should use the non-idempotent POST method as well.