GET, POST or PUT, which one to use to issue a invoice?

Asked

Viewed 820 times

5

Which should I use when the intention is not to view, save or update the data?

I’m creating an API that makes the issuance of an electronic invoice, I made the complete CRUD for the information of the notes, but now I need to create a feature that transmits the XML to the IF, like:

http://localhost/api/nfes/10/transmite

I thought to use PUT or PATCH, because the request will end up updating the Nfe status from "Pending" to "Authorized" in the database, only that there will be no body in the request... what do you think?

  • returns something?

  • only a JSON with the answers from Faz as "authorized invoice" or "rejected - inform ie of the recipient" etc

  • As you said you will "end up updating Nfe status in the database", you could delete GET from your options

  • I had thought about GET just because no data is being sent in the request, only the ID of the nfe by URI...

  • All broadcasts will be manual? Why not issue as soon as the note is generated?

  • 3

    You can define another resource for this, something like /api/emissao/<id> and make a POST for a new issue. Also, you can create a table in the bank to manage emissions and keep a history of attempts - useful for when it gives issue error.

  • Why sometimes the user inserts some incorrect information and wants to correct or want to view the PREVIOUS DANFE before transmitting... almost always there are some bureaucracies between filling and transmission, so...

  • About PUT vs PATCH, look here: https://answall.com/questions/217894/qual%C3%A9-a-difference%C3%A7a-between-m%C3%A9all-put-e-o-patch

  • @Andersoncarloswoss, I found your idea sensational, I will implement the /api/broadcast

Show 4 more comments

2 answers

4


Determining which method to use in each situation is always a delicate discussion. The implementation shall be according to the needs and limitations of the project.

Including methods, whether or not specified in a specification, RFC 7231, may vary depending on the application. There may be an API that uses the GET method to generate a new record, POST to delete, etc. But, of course, you can imagine the confusion of documenting and using this API? The less strange behaviors your application has, the easier it will be to use it.

Without knowing the details of your project, only with what was detailed in the question, I see two possible solutions:

  1. Using the PATCH method;
  2. Create your own resource;

But each with its own peculiarities:

When using the PATCH method to issue the invoice, make sure you never use the same method for anything else; otherwise you will run into problems. The PATCH method is often used for partial updates of a resource. As I said, issuing the note would only change the status of the note and, in a way, it would make sense to issue the note in PATCH. But what if you need to change other fields of the invoice? One way would be for you to issue the note only when its status is changed from any value to issued - and please use transactions here. So if it were to change any other field, such as the NCM of a product, if necessary, you could do so without worrying whether the note would be issued or not.

But I see a possible problem with this approach: what if, for some reason, a invoice is issued manually and you just want to change the status to issued? If you set the status to issued, your system will try to reset it - you can even treat the duplicate return of the note and ignore the error, but it is a strange behavior for a system to try to issue an already issued note. Someone outside, who did not participate in the system design, trying to update the status would not expect the note to be issued - she wants to partially change the feature, just, not Edit it.

In this case, I would suggest the second approach: to create an own resource only for the issuance of the invoice:

POST /api/emissoes/ HTTP/1.1

id=1

In this way, you could use the POST method to create a new issue and thus already leave open for possible/likely future implementations of an emissions history. You can create a table in the database where you store the issued invoice and the return obtained from the SEFAZ. With the GET method, you can get the return of a particular issue or a list of multiple emissions in order to generate reports.

Not to mention that you could use the PATCH method to issue correction letters and DELETE to cancel notes, for example.

But, emphasizing, that you, who is responsible for the project, who knows all the requirements and limitations, should know what is best. No chat, for example, I was asked whether in a endpoint that only accepts GET/POST/PUT, as could be done a partial update that would be done in the PATCH. As it would possibly make less sense to create a endpoint Only to partially update the resource, I suggested using the PUT itself, with an additional parameter indicating whether the update should be partial or not. It is not always the best solution, but for the problem presented, it seems to be.

And it doesn’t hurt to remember that there are many applications that survive only with GET/POST, because the HTML form is unable to send another type of request.

Note: In fact, if it’s an internal enterprise-wide API, nothing prevents you from creating custom HTTP methods - as long as the environment is properly configured. For example, you could create a method called EMIT and request: EMIT /nota-fiscal/1 HTTP/1.1. It is just not recommended to use this in public Apis, as not all customers can support custom methods.

  • Sensational, thank you for the explanation!

1

You say:

Which should I use when the intention is not to view, save or update the data?

And then at the end of the question it says:

as the request will end by updating the Nfe status from "Pending" to "Authorized" in the database

In view of this, the request will update the status of the note, if we understand what Maniero replied in that question, the ideal method for you is PUT.

That is, PUT should be used in more specific situations where you want to send information and do nothing but store it in some way.

An API that uses PUT for data update is CIELO if you consult that method, it serves similarly to what you are wanting to do and uses PUT also.

  • 2

    In this case, then, the ideal would be the PATCH, because the PUT normally waits for the complete object and overwrites the database. PATCH only updates the received data: partial update.

  • yes, I also thought about PATCH, but I believe the answer to my question has already been answered via comment by @Andersoncarloswoss

  • @Jeffersoncarlosbd of good, I will delete my answer and study the comments so I already add a little bit in knowledge hehe... anyway then put the solution and why of it as resurrected for the question not to be empty if Andersoncarloswoss does not put his.

  • 1

    don’t need to delete your answer, it may be someone else’s solution, my case is quite specific, but the question is quite generic...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.