I am a beginner and learning for example to submit a form to create an order (using POST method), edit an order (using PUT method), delete an order (using DELETE method) or search for an order (using GET method) targeting a restful web service. The order info is captured by the server script (probably PHP) and depending on the http method the order is either inserted, updated or deleted respectively. I have no knowledge of how the script does it.
My 2 questions are:
when I POST, PUT or DELETE, the server script could successfully perform the operation on the database (or) be unsuccessful in making changes to the database. How will I know if the script performed the database operation successfully or failed or there was some other error so that I can display a status to the user of the app accordingly?
(I understand that for a GET request the script sends the data requested in JSON or XML and I need to parse the JSON and display it to the app user. I just dont understand how it works for POST, PUT or DELETE. Will I receive a json for info or should I look somewhere else to able to provide a useful response to the app user).
I see POST being used instead of GET to get JSON response. My understanding is that POST is for insert operation only. Am I missing something? Why POST is being used in real time sometimes.
Thank you for your time in advance!
When the script finishes you send back a status code (200 is okay, 500-Internal server error, 404-Not found etc) and a message (json in your case)
"I see POST being used instead of GET to get JSON response", depends on what type of processing you are referring to. If you need a resource (a specific entity) you make a GET request, but if you need to make a custom process or a search then POST might be good for you
P.S: A good read on implementing a API an here. Check out the status codes used in the example, you can change your implementation based on your needs
Related
I am new to the term webhook, and i understand that it is a normal HTTP POST request, so when i send the data to an endpoint it utilizes this data and i can test this on a tool like (webhook.site), and what is noticeable on webhook.site it shows me directly the posted data on the real time. I need some help to know how this real time updates can be done using a custom webhook. If i created a PHP file that with will receive a posted data i don't know how to update the page upon request automatically.
I'm using Laravel 5.3.
Essentially, when a user clicks a button on the screen, I need to get data from the database (using AJAX), and then display that data on the screen.
However, I'm not sure if I should be using a GET or POST request? I've only ever used GET requests for routing when the user wants to get to a specific page, like a GET request for /index or /profile.
Which should I use?
There is a difference between GET & POST method in Laravel
GET is used when we want to get some data from the server and we do not send any parameter in request. And the security threat is not a concern, like you are opening a page on browser
POST is used when we want to send some parameter to the server and based on that parameter some processing is done. In laravel it is mandatory to include CSRF token wit the request for security concern.
So choose as per your requirement.
easy! Use GET when you're to getting data, and POST when you're posting data.
There are even more of these request methods (or verbs, if you like). For example a PUT request to edit data, DELETE request to delete data etc. However, these aren't supported in most browsers yet, but i know laravel has a clever workaround so you can use them anyway. check this links:
https://laravel.com/docs/5.3/routing
This is actually something of your own choosing. If the operation is a sensitive one you might consider using POST so that you can have protection over CROSS-SITE REQUEST FORGERY from attackers but if not so you can simply use GET
If you only want save data in database(no return data) so you should use POST. And whenever you want to get data from database so you should use GET.
Ex - If you want to insert a new user information in database so here you use GET method and if you want to edit existing user information and return updated information so you will use GET method.
I'm currently rather new to PHP and working on a project for my job that will take information about leads when they are updated from the sharpspring API and put it into Insightly's API. Sharpsrings API has a function, subscribeToLeadUpdates, that takes a url as a parameter and says it will POST the list of leads to the url whenever it is updated but I am confused about how exactly this works and how I would work with that information. Each time it POSTs will it run the script for the url that I gave it? I subscribed a url of a droplet I set up to test it with a dummy script that should just create a file whenever it is ran, and then tried testing it by updating a lead but nothing happened. Also if I update a lead and then run the script manually with a var_dump($_POST) it just dumps an empty array. Any help is greatly appreciated
I ran their subscribe method and it returned
"{"result":{"success":true},"error":null,"id":""}"
That is correct, and means your request was accepted as valid.
There has to be some kind of activity with a lead that would trigger them trying to POST data to URL you sent...
My question maybe unheard of or may even be impractical.
But, I believe it is practical and acceptable.
Problem: PHP request and response in background thread.
Problem constraints:
You know it uses POST method.
It has two fields fname and lname as html ids that need to be filled.
You get response in the same page i.e. index.php .
Pseudocode to solve the problem:
Open the website in background. Something like openURL("xyz(dot)com");
Read the data sent in html format.
Change the value of the fields fname and lname to required fields "Allen" and "Walker."
Now, submit the fields back to the server. ( Page has a submit button.)
Note: PHP code has a standard if-else check to check for those values. If the field is properly set, it then says "Success" else "Failed"
Again, get the response sent by the server.
Check if "Success" was returned.
If "success" was returned, UPDATE UI thread for saying "JOB done".
Else, "Job failed."
Note: I am not talking about using a WebView. Everything happens via a service or AsyncTask.
Now, this is a simple question, and any ideas or a direction is acceptable.
So, to recap. You have to open a webpage in background, change its field, submit it back and then get the response.
I know we can open a website and get its content. But, I would also like to know if what I said is possible or not. And, I know it is possible. And, that I only lack the knowledge on Java Web API, could you please guide me on this.
Thank You. And, Have a Good day!
use this link for best solution of calling web service without using WebView.
In this example fname and lname sent in namevaluepairs we get responce in json formet and parse json and get data also json parse defined in examples.
What you are trying to achieve is to create a webservice call using a AsyncTask.
Like you have mentioned, you can make a post request within the AsyncTask, passing in the variables required for the request.
You can find a good example as shown: Sending HTTP Post Request with Android
I'm having an API on which I need to set custom status code. Apart from the normal standard http status codes, is it the best practice to create our own status code?
i.e, If my client is sending a blank parameter (orderID), can I set a random status code number of my own say 123? Or is there any standard way to create customer status codes?
There are quite a lot of existing codes, so usually you won't need to invent new ones. You might, though, but when you do, make sure to use the right ranges.
For instance, the 200+ range indicates success of any kind, 300+ is a redirect, 400+ range is a client error (bad url format, target not found), and 500+ is a server error.
By following these guidelines, you can use all kinds of clients to communicate to your api. A browser should normally display the results of any 2XX status code, and treat it as a succesful request, even if the particular XX is unknown to it.
For a rather complete list of status codes and their normal meaning, as well as a general description of each of the ranges, see this:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
599 is the highest assigned status code. I wouldn't know if it's allowed to use the 600+ range, but I cannot find anything about it. I cannot imagine that your status wouldn't fit in any of the existing categories, though.
REST:
Aside from the return code, you can (should?) also have a look at the different methods. You probably know GET (sending data in url) and POST (sending extra data with the request), because they are commonly used. But you also got PUT and DELETE, which are especially useful for an api like this. If you search for terms like Restfull API, you should get plenty of documentation about this subject. Also, see this W3 link for an overview of request methods.
Bringing it together:
For creating a customer through the api, you could send a PUT request with the data of the new customer. The api can return 201 Created with an ID (or slug) for the customer. If you want to delete the customer, send a DELETE request and return 204 No content, since you just need to confirm that the delete succeeded, without sending any content.
Or is there any standard way to create customer status codes?
No. I'd go for a generic HTTP error as 400 Bad Request and provide a meaningful error message in the response body. This wasy you don't have to make up HTTP codes as you go, and your consumers can process different error responses identically.
Don't define custom status codes unless you're willing to going through the process of standardizing them (http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p2-semantics-26.html#considerations.for.new.status.codes).
Otherwise just use a generic one and send additional details in the response body.