News for hackers. Stuff that matters.
Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by iio7 on Tuesday August 16, 2022 @08:01PM (#32490677)
First of, just like the term "hacker" became popularized by the media and turned into the meaning of someone who cracks computers and software, so has HTTP based APIs been popularized and turned into REST applications, but this is wrong. A HTTP based API cannot, by its very nature, ever be REST.

Now that's out of the way, yes. We use Go for both API and web development, actually full stack WITHOUT any JavaScript. We have written a custom error handler that deals with runtime HTML template errors, if any, which improves the otherwise tedious work when a template makes the web service crash.

Go is superb at HTTP and all web related. We still have some PHP development, but would very much like to do Go all the way, we just haven't gotten there yet.

Last, but not least, we do not use any frameworks or libraries, only the Go standard library and we love it. No matter were we put it, it just always works and is very performant.

Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by bern4444 on Wednesday August 17, 2022 @12:56AM (#32492520)
After reading the article you linked in another sibling comment's response, can you point to an example of what a true RESTful service looks like.

I see how under that definition an API can't be RESTful, but what service then can be? And how do clients interact with it?

Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by virtualmic on Wednesday August 17, 2022 @02:30AM (#32492984)
> can you point to an example of what a true RESTful service looks like.

Not OP, but there was an interesting article & discussion recently here: https://news.ycombinator.com/item?id=32141027

Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by iio7 on Wednesday August 17, 2022 @08:35AM (#32495419)
Basically a normal HTML website, that's true REST.
Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by bern4444 on Wednesday August 17, 2022 @12:21PM (#32498347)
So if I made an API that would include in its responses other endpoints that can be hit, would that make it RESTful?

An example response could be

  {
    name: 'john',
    nickName: 'johnny',
    relatedEndpoints: ['/v2/friends', '/v2/jobs/', ...]
  }
Or one could include a link to an Open API spec for the API in the response of every request that details all endpoints available and their parameters/bodies.

  {
    name: 'john',
    nickName: 'johnny',
    apiSpec: '/v2/open-api-spec'
  }
Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by iio7 on Wednesday August 17, 2022 @01:15PM (#32499073)
No, that would not make it REST. I suspect you didn't read the article thoroughly?

If your application needs documentation, i.e. a spec or similar, also referred to by some as a "profile", in order for the client to use your resources beyond the initial root URI, then that is not REST!

An API, no matter how you twist or turn it, can never be REST. REST are truly only for manual human consumption, not machines. That is why it is so weird everyone is calling their APIs for REST.

Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by bern4444 on Wednesday August 17, 2022 @05:13PM (#32501812)
I read the article fully.

If an API response included all the possible actions and information that one takes as a follow up to a previous call, how is that any different from providing the equivalent forms and links to additional information on a web page rendered by a browser?

It certainly seems possible to pigeonhole an API into a RESTful architecture but I understand why based on the article, APIs aren't often considered RESTful.

But it seems they can be. I see this as a proof by contradiction.

I, today, can navigate directly to posts by a friend of mine on a social media site. Auto logged in, not having to enter any form data etc. That's my starting point both in a browser and via API.

I'd see their post. Alongside that post I'd see actions I can take: clicking on other friends' profiles, liking the post, commenting on the post, removing the friend etc.

Encoding all that in an API might look like this:

  {
    name: 'john',
    userId: '12'
    nickName: 'johnny',
    posts: [
      { content: 'My funny comment', likes: 3, likedByUserIds: ['13', '14', '15' ], postId: 333 },
      { ... }
    ]
    relatedEndpoints: [
      '/users/12/friends/', : {}
      '/users/13', : {}
      '/users/14', : {}
      '/users/15', : {}
      '/like/333, : {}
      '/unlike/333', : {}
      '/remove-friend/12', : {}
      '/comment/333/' : { commentText: '' }
      ...
    ]
  }
In this way, the response to my request includes all the possible actions I can then take and what data I need to provide to take it (in the example of the comment route).

* No consulting of external documentation is needed, all the information is right there. *

I can explore another user's profile (people with user ids of 13, 14, and 15), I can like and unlike this post with the `/like/` and `/unlike` routes, I can remove this person from my friends list etc.

Including such information makes this response no different than what I can do via browsing the page - all my interactions with the API are directly driven by their responses - not by consuming outside documentation.

If all possible actions and their routes were included on route supported by my API, why would this not be RESTful?

At the end of the day - this to me is a distinction without a difference. Whether this documentation is defined as part of the response or stored in an OpenAPI spec, the end result is the same - I can easily wire things up together to accomplish my goals.

This is where SDKs become more useful.

SDKs are fully documented, require no external information for them to be used, contain everything possible that can be done as methods etc.

If I get a `User` class back from an SDK - I can then invoke all the methods on that user like - getFriends, getPostById, etc etc.

If I call a `getFriendById(12)` method I'd expect a `Friend` instance rather than a more generic `User` instance which should have a `removeFriend` method but no `addFriend` method. That appears equally as RESTful as a website with actions that can be taken.

Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by iio7 on Wednesday August 17, 2022 @11:12PM (#32504825)
You're missing the point. If you provide an API like in the example you give, put it out on the net at some domain, then in order for it to be REST, I MYST be able to access that domain, without any prior knowledge whatsoever, and make a request.

When I do that in the case you provide, I would not know anything about how to use those resources. Without any prior knowledge, I don't know what "users" are, or what a "like" is or an "unlike". In order for me to use this resource, I need documentation. Even if you provide the documentation in the response to the request, and I have to read that first, then it is NOT REST.

By the very nature of an API, it can never be REST.

Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by bern4444 on Thursday August 18, 2022 @10:39AM (#32510299)
Okay, I understand the distinction but as I said, it seems to be a distinction without a difference.

The idea of REST has evolved beyond the initial definition to include a broader and far more impactful meaning. The abstraction of UI over data. The important piece is the data.

I could make an entire website, entry point and all that only returned json and included the links for actions users could take and other pages they could visit. Whether I use a browser to see that or my terminal with a cURL makes no difference.

Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by Cwizard on Wednesday August 17, 2022 @04:21AM (#32493725)
Can I ask about the choice to not use any libraries? As in your go.mod file is empty? I understand the desire to keep dependencies to a minimum but surely there are use cases where it is better to import existing code rather than writing something from scratch no?

Do you use internal libraries?

Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by iio7 on Wednesday August 17, 2022 @08:44AM (#32495521)
We basically only use the Go standard library. So far we have found no use cases where that didn't cover us well enough.

This also removes any problems with third party dependencies that suddenly gets abandoned yet still have serious issues, supply chain attack problems, etc.

We have also found that most third party libraries tries to be (more or less) general purpose, so they often contain lots of stuff we would never need, so we prefer to e.g. write our own specific code.

The way we work require a little extra effort because we write the things we need our selves, but of course we reuse our HTTP handler code, our custom runtime error handler code, etc., from project to project, but in general the benefits far outweigh the cost in our experience. The fewer dependencies, the fewer problems - and Go shines in this because of how much is covered in the standard library.

Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by Cwizard on Wednesday August 17, 2022 @10:00AM (#32496503)
Makes sense, thanks for taking the time.
Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by athorax on Tuesday August 16, 2022 @09:49PM (#32491350)
Can you elaborate on why HTTP APIs cannot be REST APIs? I know a lot of http services are misclassified as REST, but why can they by definition not be?
Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by iio7 on Tuesday August 16, 2022 @10:12PM (#32491501)
This is one of my favorite posts about it.

https://www.unixsheikh.com/articles/no-your-api-isnt-rest.ht...

The author got Roy T. Fielding, one of the principle authors of the HTTP specification and the originator of the Representational State Transfer (REST), to comment on this issue by email (was since removed when the author changed his blog).

Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by athorax on Wednesday August 17, 2022 @08:52AM (#32495620)
That was a super interesting read, thanks for sharing!
Re:Ask HN: Anyone here switch to Go for building (REST) APIs? by nesarkvechnep on Wednesday August 17, 2022 @01:29AM (#32492688)
I’m glad some people, even though not many, still know what REST implies.