I've been working with django restframework, I have not managed to make a PUT or a POST to JSON, I could only make a post with the form of the rest django web interface framework
As I can make a PUT, POST to be called from an app
It's not about Django. All the other HTTP methods except GET, e.g. PUT, POST, DELETE etc require a form to simulate. Because when the HTTP request is made, the request needs to mention what type of request it is. When you enter an URL on the browser address bar, it's always GET. You can write a form and modify it's method attribute to say if it's PUT, POST etc. Other ways to request those methods is to use a http library like requests or simple Javascript
var xmlhttp;
function test(){
execute('GET', 'http://server.com/testServer.php');
execute('POST', 'http://server.com/testServer.php');
execute('PUT', 'http://server.com/testServer.php');
execute('DELETE', 'http://server.com/testServer.php');
}
function execute($method,$url){
xmlhttp=new XMLHttpRequest();
xmlhttp.open($method,$url,true)
xmlhttp.send(null);
}
You can read this question for more details. JS snippet taken from here.
Related
I'm trying to build a simple web application.
What it does is: using ajax to post a request to server with post method.
The controller is work well with GET method, but when I changed it to POST method, it always return 404 (Not Found).
Is there any solution or suggestion for check?
This is the solution for this problem,
You need to include csrf token in the header of AJAX request, because by default Spring enable CSRF security feature.
You can find more information here: Ajax and JSON Requests
I read the following text from Apress Apache Tomcat 7
The HttpServlet.service() method implementation is a convenient way to
control access to your servlets in the code. For example, servlets
that delete data from the database should always be accessed using the
DELETE method, but because browsers only support GET and POST
operations, the POST method should be used instead.
It seems most browsers only supports 2 HTTP methods, GET and POST.
If so, why?
In a form the only possible methods are GET and POST. When you make an AJAX call, other methods are supported.
<form>:
method = get|post [CI]
This attribute specifies which HTTP method will be used to submit the form data set. Possible (case-insensitive) values are "get" (the
default) and "post".
XMLHttpRequest:
method
The HTTP method to use, such as "GET", "POST", "PUT", "DELETE", etc. Ignored for non-HTTP(S) URLs.
There are modern browsers which allow GET, POST, PUT and DELETE using the XMLHttpRequest. You can go through this bug 10671
Executing PUT and DELETE to modify resources on the origin server is
straight-forward for modern Web browsers using the XmlHttpRequest
object. For unscripted browser interactions this not so simple.
Typically, devs and frameworks end up creating work-arounds that mimic
the HTTP PUT/DELETE + Etag interaction using a "POST FORM" coupled
with specialized server-side code to sort out the special case and act
as if the proper HTTP Method was used in the request
Other considerations:
Using POST as a tunnel instead of using PUT/DELETE can lead to
caching mis-matches (e.g. POST responses are cachable5, PUT responses
are not[6], DELETE responses are not[7])
Using a non-idempotent method (POST) to perform an idempotent
operation (PUT/DELETE) complicates recovery due to network failures
(e.g. "Is is safe to repeat this action?").
You can also refer this thread: Are the PUT, DELETE, HEAD, etc methods available in most web browsers?
Is there a tool which enables me to prepare a custom HTTP request. I need to request ressources which are bound to custom HTTP methods on server side.
I know about the fire fox plugin RESTClient which is pretty perfect, except that I can't set a custom HTTP method type like: FOO.
EDIT
I found out that the RESTClient plugin also provides the possibility to create custom HTTP methods. It's the same as with Fiddler. But however, Fiddler is a nice alternative.
I would suggest something similar to fiddler
Postman supports custom HTTP methods...
Source: https://github.com/postmanlabs/postman-app-support/issues/166
I want to encode a URL such that it sends a POST request to a server. is that possible? and if so, how? I have searched around and mostly found that appending parameters to a url only sends them as parameters for GET request. is there a way to do that for POST request?
basically, i am trying to implement a CSRF (not for malicious but testing purposes) and i want to be able to send a POST request to a server by encoding my url.
GET and POST are HTTP methods. In GET the request parameters are taken as query string in the request URL. In POST the request parameters are taken as query string in the request body.
So you need to instruct whatever tool you're using to pass the parameters through the request body instead of the request URL along with a HTTP method of POST instead of (usually the default) GET.
Either way, the parameters just needs to be URL encoded anyway. There's no difference for POST or GET, unless you're setting the content encoding to multipart/form-data instead of (usually the default) application/x-www-form-urlencoded.
If you give more details about what programming language and/or library and/or framework you're using, then we may be able to give a more detailed answer how to invoke a HTTP POST request.
No.
The method is not part of the Url. You'd have to make the request in such a way that it uses the post method.
You didn't mention any details, but if it's from inside a document in the browser, you can either use a form:
<FORM action="someUrl.htm" method="post">
You can make a link that will send the form by javascript:
<form action="http://www.example.com/?param=value" method="post" id="someForm">
link
</form>
or an XmlHttpRequest with javascript:
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
...
I am using jQuery to get back some JSON data from the server. I am using a POST verb, but even after setting the WebMethod CacheDuration attribute, the JSON doesn't get cached.
I checked the response headers with firebug and the Cache-Control is still set to no-cache.
How can i cache these request on the client and avoid the server to be hit every time.
UPDATE
After reading this post from scottGu I thought it would have been safe to go on to use a POST request. Does his post not apply to the kind of operation i would be trying to do ? (getting data from the server without modifying it). In fact after changing the verb to GET, i am not even getting to the web service ...
You should be using a get request. Post does not cache by default. You can try to get a post to cache by using .ajax() and setting cache to true and type to post. I cannot say that this will work as typically you would not expect a post to cache. I suggest using get.
E.g
$.ajax( { url: '/bla',
type : 'post',
data : dataObj,
cache : true } );
Use the GET keyword instead if you want to use caching. There is a similar question on SO.
I've noticed differences in the way ASP.NET caches responses depending upon whether the caching parameters are query string parameters or (in my case with ASP.NET MVC) route parameters.
I never completely figured out exactly what the conditions were, but this may help someone.