Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 657795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:51:47+00:00 2026-05-13T22:51:47+00:00

I’m working on a REST API. The key objects ("nouns") are "items", and each

  • 0

I’m working on a REST API. The key objects ("nouns") are "items", and each item has a unique ID. E.g. to get info on the item with ID foo:

GET http://api.example.com/v1/item/foo

New items can be created, but the client doesn’t get to pick the ID. Instead, the client sends some info that represents that item. So to create a new item:

POST http://api.example.com/v1/item/
hello=world&hokey=pokey

With that command, the server checks if we already have an item for the info hello=world&hokey=pokey. So there are two cases here.

Case 1: the item doesn’t exist; it’s created. This case is easy.

201 Created
Location: http://api.example.com/v1/item/bar

Case 2: the item already exists. Here’s where I’m struggling… not sure what’s the best redirect code to use.

301 Moved Permanently? 302 Found? 303 See Other? 307 Temporary Redirect?
Location: http://api.example.com/v1/item/foo

I’ve studied the Wikipedia descriptions and RFC 2616, and none of these seem to be perfect. Here are the specific characteristics I’m looking for in this case:

The redirect is permanent, as the ID will never change. So for efficiency, the client can and should make all future requests to the ID endpoint directly. This suggests 301, as the other three are meant to be temporary.

The redirect should use GET, even though this request is POST. This suggests 303, as all others are technically supposed to re-use the POST method. In practice, browsers will use GET for 301 and 302, but this is a REST API, not a website meant to be used by regular users in browsers.

It should be broadly usable and easy to play with. Specifically, 303 is HTTP/1.1 whereas 301 and 302 are HTTP/1.0. I’m not sure how much of an issue this is.

At this point, I’m leaning towards 303 just to be semantically correct (use GET, don’t re-POST) and just suck it up on the "temporary" part. But I’m not sure if 302 would be better since in practice it’s been the same behavior as 303, but without requiring HTTP/1.1. But if I go down that line, I wonder if 301 is even better for the same reason plus the "permanent" part.

Thoughts appreciated!


Edit: Let me try to better explain the semantics of this "get or create" operation with a more concrete example: URL shortening. This is actually much closer to my app anyway.

For URL shorteners, the most common operation by far is retrieving by ID. E.g. for http://bit.ly/4Agih5, bit.ly receives an ID of 4Agih5 and must redirect the user to its corresponding URL.

bit.ly already has an API, but it’s not truly RESTful. For the sake of example, let me make up a more RESTful API. For example, querying the ID might return all sorts of info about it (e.g. analytics):

GET http://api.bit.ly/item/4Agih5

Now if I want to submit a new URL to bit.ly to shorten, I don’t know the ID of my URL in advance, so I can’t use PUT. I’d use POST instead.

POST http://api.bit.ly/item/
url=http://stackoverflow.com/ (but encoded)

If bit.ly hasn’t seen this URL before, it’ll create a new ID for it and redirect me via 201 Created to the new ID. But if it has seen that URL, it’ll still redirect me without making a change. This way, I can hit that redirect location either way to get the info/metadata on the shortened URL.

Like this example of URL shortening, in my app, collisions don’t matter. One URL maps to one ID, and that’s it. So it doesn’t really matter if the URL has been shortened before or not; either way, it makes sense to point the client to the ID for it, whether that ID needs to be created first or not.

So I probably won’t be changing this approach; I’m just asking about the best redirect method for it. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:51:48+00:00Added an answer on May 13, 2026 at 10:51 pm

    I’d argue for 303. Supposing right now hello=world&hokey=pokey uniquely identifies item foo, but later item foo’s hokey value changes to “smokey”? Now those original values are no longer a unique identifier for that resource. I’d argue that a temporary redirect is appropriate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm making a simple page using Google Maps API 3. My first. One marker
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.