Rectangle 27 4

I do not know your complete code, and I am not an expert in AngularJS, but it looks like your want to send a DELETE request to the URL <hopefullySomething>/{id} (Path variable). But it looks like that you send a DELETE request so some URL with an parameter id <hopefullySomething>?id={id} (Request parameter).

This question and answers explain the difference between path variable and request parameters a bit more @RequestParam vs @PathVariable

java - Error 405 Method Not Allowed error, when sending DELETE to serv...

java angularjs spring rest crud
Rectangle 27 4

I do not know your complete code, and I am not an expert in AngularJS, but it looks like your want to send a DELETE request to the URL <hopefullySomething>/{id} (Path variable). But it looks like that you send a DELETE request so some URL with an parameter id <hopefullySomething>?id={id} (Request parameter).

This question and answers explain the difference between path variable and request parameters a bit more @RequestParam vs @PathVariable

java - Error 405 Method Not Allowed error, when sending DELETE to serv...

java angularjs spring rest crud
Rectangle 27 13

Based on the code in Jersey 2.18 version, The class JerseyInvocation use a predefined HashMap to validate HTTP method and its Entity as below:

map.put("DELETE", EntityPresence.MUST_BE_NULL);
map.put("GET", EntityPresence.MUST_BE_NULL);
...

While, please note that it also provide a Jersey client configuration property ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION to determine whether to stop the rest execution or not, so here we can use this property to suppress validation in order to continue to send a DELETE request with Entity. e.g.

ClientConfig config = new ClientConfig();
    config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
    Client client = ClientBuilder.newClient(config);
    ...

I have a similar need to do this, although in my case I don't actually have a body to send, but I do need to force a 'Content-Length: 0' to be sent with a DELETE. Trying this suggestion with Jersey Client 2.5.2, I get a ProtocolException: java.net.ProtocolException: HTTP method DELETE doesn't support output at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1081) at org.glassfish.jersey.client.HttpUrlConnector$3.getOutputStream(HttpUrlConnector.java:267)

java - How to send enclose data in DELETE request in Jersey client? - ...

java rest jersey jax-rs
Rectangle 27 14

You have to send a DELETE request to

http://[your_host]:9200/[your_index_name_here]

You can also delete a single document:

http://[your_host]:9200/[your_index_name_here]/[your_type_here]/[your_doc_id]

After deleting you can look up if the index still exists with the following URL: http://[your_host]:9200/_stats/

what is the way to delete indices older than 10 days ? I can not use curator because my server is not support.

Removing Data From ElasticSearch - Stack Overflow

elasticsearch
Rectangle 27 3

A DELETE with an entity body is not strictly forbidden but it's very uncommon and ignored by some frameworks/servers. The need of an entity body may indicate that a DELETE is not used as it is intended.

GET /customers/4711
DELETE /customers/4711
GET
404

identified by a URL

Your URL /store/remove/from/group does not seem to identify a resource. Using identifiers like /store/4711 or /groups/4711 and sending a DELETE on them would not fit your needs because you want to "remove a store from a group" not delete a store or a group.

Assuming you have a group resource

and you want a result like

{
  "id" : 4711,
  "stores" : [123, 789]
}

you are not deleting anything. You are modifying a resource so PUT, POST or PATCH are appropiate methods. JSON-Patch is a good format for describing such changes. A request would look like this:

PATCH /groups/4711 HTTP/1.1
Content-Type: application/json-patch

[
  {
    "op" : "remove"
    "path" : "stores/1"
  }
]

Thanks for your suggestions. It seems that PATCH method is not currently supported by Jersey framework. Also does Java web server like Tomcat or Jetty support HTTP PATCH method? I could use PUT request in this case since I'm modifying the resource.

I'm sorry, I don't have an overview which container or framework supports PATCH. PUT is a good idea but this has the downside that you need to send the whole modified resource.

Thanks. For the sake of stability, I will just use http PUT for now and hopefully http PATCH method will be officially included in later Jersey release.

java - How to send enclose data in DELETE request in Jersey client? - ...

java rest jersey jax-rs
Rectangle 27 3

resteasy
JAX RS Client API
build().invoke()
client.target("$baseUrl$restEndPoint/$entityId")
                .request("application/json")
                .build("DELETE", Entity.entity(entity, MediaType.APPLICATION_JSON))
                .invoke()

But it does not work with jersey

There is validateHttpMethodAndEntity function in JerseyInvocation which validates method and entity presence. This will throw IllegalStateException

@pshirishreddy, I use resteasy implementation of JAX RS Client API. And it works.

Agreed but the OP question was related to jersey client :)

java - How to send enclose data in DELETE request in Jersey client? - ...

java rest jersey jax-rs
Rectangle 27 8

As per RequestOptionsArgs interface and Http delete function argument, i think you need to send delete request as below :

deletetag(tagid: number): Observable<any> {

  let body = JSON.stringify(
      {
        "token": "test",
        "content": {
        "tagId": tagid
      }
    }
  );
  let headers = new Headers({ 'Content-Type': 'application/json' });
  let options = new RequestOptions({
    headers: headers,
    body : body
  });

  return this.http.delete("http://localhost:8080/backend/tag", options)
        .map(res => this.extractData(res))
        .catch(this.handleError);
}

Although this may work in angular, you may have issues server side with this: stackoverflow.com/a/25237188/3806701

angular2 services - Angular 2 Http delete send json in body - Stack Ov...

angular angular2-services
Rectangle 27 4

I was having this problem on a django app I'm tinkering with (disclaimer: I'm tinkering to learn, and am in no way an expert). What I wanted to do was use jQuery ajax to send a DELETE request to a resource, delete it on the server side, then send a redirect back to (basically) the homepage. When I sent HttpResponseRedirect('/the-redirect/') from the python script, jQuery's ajax method was receiving 200 instead of 302. So, what I did was to send a response of 300 with:

response = HttpResponse(status='300')
response['Location'] = '/the-redirect/' 
return  response

Then I sent/handled the request on the client with jQuery.ajax like so:

<button onclick="*the-jquery*">Delete</button>

where *the-jquery* =
$.ajax({ 
  type: 'DELETE', 
  url: '/resource-url/', 
  complete: function(jqxhr){ 
    window.location = jqxhr.getResponseHeader('Location'); 
  } 
});

Maybe using 300 isn't "right", but at least it worked just like I wanted it to.

PS :this was a huge pain to edit on the mobile version of SO. Stupid ISP put my service cancellation request through right when I was done with my answer!

javascript - How to manage a redirect request after a jQuery Ajax call...

javascript jquery ajax redirect
Rectangle 27 4

I was having this problem on a django app I'm tinkering with (disclaimer: I'm tinkering to learn, and am in no way an expert). What I wanted to do was use jQuery ajax to send a DELETE request to a resource, delete it on the server side, then send a redirect back to (basically) the homepage. When I sent HttpResponseRedirect('/the-redirect/') from the python script, jQuery's ajax method was receiving 200 instead of 302. So, what I did was to send a response of 300 with:

response = HttpResponse(status='300')
response['Location'] = '/the-redirect/' 
return  response

Then I sent/handled the request on the client with jQuery.ajax like so:

<button onclick="*the-jquery*">Delete</button>

where *the-jquery* =
$.ajax({ 
  type: 'DELETE', 
  url: '/resource-url/', 
  complete: function(jqxhr){ 
    window.location = jqxhr.getResponseHeader('Location'); 
  } 
});

Maybe using 300 isn't "right", but at least it worked just like I wanted it to.

PS :this was a huge pain to edit on the mobile version of SO. Stupid ISP put my service cancellation request through right when I was done with my answer!

javascript - How to manage a redirect request after a jQuery Ajax call...

javascript jquery ajax redirect
Rectangle 27 4

I was having this problem on a django app I'm tinkering with (disclaimer: I'm tinkering to learn, and am in no way an expert). What I wanted to do was use jQuery ajax to send a DELETE request to a resource, delete it on the server side, then send a redirect back to (basically) the homepage. When I sent HttpResponseRedirect('/the-redirect/') from the python script, jQuery's ajax method was receiving 200 instead of 302. So, what I did was to send a response of 300 with:

response = HttpResponse(status='300')
response['Location'] = '/the-redirect/' 
return  response

Then I sent/handled the request on the client with jQuery.ajax like so:

<button onclick="*the-jquery*">Delete</button>

where *the-jquery* =
$.ajax({ 
  type: 'DELETE', 
  url: '/resource-url/', 
  complete: function(jqxhr){ 
    window.location = jqxhr.getResponseHeader('Location'); 
  } 
});

Maybe using 300 isn't "right", but at least it worked just like I wanted it to.

PS :this was a huge pain to edit on the mobile version of SO. Stupid ISP put my service cancellation request through right when I was done with my answer!

javascript - How to manage a redirect request after a jQuery Ajax call...

javascript jquery ajax redirect
Rectangle 27 4

I was having this problem on a django app I'm tinkering with (disclaimer: I'm tinkering to learn, and am in no way an expert). What I wanted to do was use jQuery ajax to send a DELETE request to a resource, delete it on the server side, then send a redirect back to (basically) the homepage. When I sent HttpResponseRedirect('/the-redirect/') from the python script, jQuery's ajax method was receiving 200 instead of 302. So, what I did was to send a response of 300 with:

response = HttpResponse(status='300')
response['Location'] = '/the-redirect/' 
return  response

Then I sent/handled the request on the client with jQuery.ajax like so:

<button onclick="*the-jquery*">Delete</button>

where *the-jquery* =
$.ajax({ 
  type: 'DELETE', 
  url: '/resource-url/', 
  complete: function(jqxhr){ 
    window.location = jqxhr.getResponseHeader('Location'); 
  } 
});

Maybe using 300 isn't "right", but at least it worked just like I wanted it to.

PS :this was a huge pain to edit on the mobile version of SO. Stupid ISP put my service cancellation request through right when I was done with my answer!

javascript - How to manage a redirect request after a jQuery Ajax call...

javascript jquery ajax redirect
Rectangle 27 4

I was having this problem on a django app I'm tinkering with (disclaimer: I'm tinkering to learn, and am in no way an expert). What I wanted to do was use jQuery ajax to send a DELETE request to a resource, delete it on the server side, then send a redirect back to (basically) the homepage. When I sent HttpResponseRedirect('/the-redirect/') from the python script, jQuery's ajax method was receiving 200 instead of 302. So, what I did was to send a response of 300 with:

response = HttpResponse(status='300')
response['Location'] = '/the-redirect/' 
return  response

Then I sent/handled the request on the client with jQuery.ajax like so:

<button onclick="*the-jquery*">Delete</button>

where *the-jquery* =
$.ajax({ 
  type: 'DELETE', 
  url: '/resource-url/', 
  complete: function(jqxhr){ 
    window.location = jqxhr.getResponseHeader('Location'); 
  } 
});

Maybe using 300 isn't "right", but at least it worked just like I wanted it to.

PS :this was a huge pain to edit on the mobile version of SO. Stupid ISP put my service cancellation request through right when I was done with my answer!

javascript - How to manage a redirect request after a jQuery Ajax call...

javascript jquery ajax redirect
Rectangle 27 4

I was having this problem on a django app I'm tinkering with (disclaimer: I'm tinkering to learn, and am in no way an expert). What I wanted to do was use jQuery ajax to send a DELETE request to a resource, delete it on the server side, then send a redirect back to (basically) the homepage. When I sent HttpResponseRedirect('/the-redirect/') from the python script, jQuery's ajax method was receiving 200 instead of 302. So, what I did was to send a response of 300 with:

response = HttpResponse(status='300')
response['Location'] = '/the-redirect/' 
return  response

Then I sent/handled the request on the client with jQuery.ajax like so:

<button onclick="*the-jquery*">Delete</button>

where *the-jquery* =
$.ajax({ 
  type: 'DELETE', 
  url: '/resource-url/', 
  complete: function(jqxhr){ 
    window.location = jqxhr.getResponseHeader('Location'); 
  } 
});

Maybe using 300 isn't "right", but at least it worked just like I wanted it to.

PS :this was a huge pain to edit on the mobile version of SO. Stupid ISP put my service cancellation request through right when I was done with my answer!

javascript - How to manage a redirect request after a jQuery Ajax call...

javascript jquery ajax redirect
Rectangle 27 4

I was having this problem on a django app I'm tinkering with (disclaimer: I'm tinkering to learn, and am in no way an expert). What I wanted to do was use jQuery ajax to send a DELETE request to a resource, delete it on the server side, then send a redirect back to (basically) the homepage. When I sent HttpResponseRedirect('/the-redirect/') from the python script, jQuery's ajax method was receiving 200 instead of 302. So, what I did was to send a response of 300 with:

response = HttpResponse(status='300')
response['Location'] = '/the-redirect/' 
return  response

Then I sent/handled the request on the client with jQuery.ajax like so:

<button onclick="*the-jquery*">Delete</button>

where *the-jquery* =
$.ajax({ 
  type: 'DELETE', 
  url: '/resource-url/', 
  complete: function(jqxhr){ 
    window.location = jqxhr.getResponseHeader('Location'); 
  } 
});

Maybe using 300 isn't "right", but at least it worked just like I wanted it to.

PS :this was a huge pain to edit on the mobile version of SO. Stupid ISP put my service cancellation request through right when I was done with my answer!

javascript - How to manage a redirect request after a jQuery Ajax call...

javascript jquery ajax redirect
Rectangle 27 24

  • Once you have authenticated the user and stored the session_id locally, send the session_id in the header of each of your http requests. That way, you're not sending the credentials with each request, but the session id. And if something happens on the server side to the session, the transaction will not be allowed.
  • When logging out, don't just delete the session_id on your app (client) side. Send a logout to the server as well so that the session can be killed server side.
  • If the session is killed on the server side, you'll want to do 1 of 2 things A) prompt the user to re-login. B) Use the store credentials to log back in, create a new session id and store it again in your singleton.

I don't think he is making any more http requests other than the initial login one. Most of his functionality seems local. +1 for mentioning the correct way to do authenticated requests.

java - Android App Strategy for keeping track of a login session - Sta...

java android session login httpurlconnection
Rectangle 27 24

  • Once you have authenticated the user and stored the session_id locally, send the session_id in the header of each of your http requests. That way, you're not sending the credentials with each request, but the session id. And if something happens on the server side to the session, the transaction will not be allowed.
  • When logging out, don't just delete the session_id on your app (client) side. Send a logout to the server as well so that the session can be killed server side.
  • If the session is killed on the server side, you'll want to do 1 of 2 things A) prompt the user to re-login. B) Use the store credentials to log back in, create a new session id and store it again in your singleton.

I don't think he is making any more http requests other than the initial login one. Most of his functionality seems local. +1 for mentioning the correct way to do authenticated requests.

java - Android App Strategy for keeping track of a login session - Sta...

java android session login httpurlconnection
Rectangle 27 353

Update: Before continuing everyone should read and understand the html5rocks tutorial on CORS. It is easy to understand and very clear.

If you control the server being POSTed, simply leverage the "Cross-Origin Resource Sharing standard" by setting response headers on the server. This answer is discussed in other answers in this thread, but not very clearly in my opinion.

In short here is how you accomplish the cross domain POST from from.com/1.html to to.com/postHere.php (using PHP as an example). Note: you only need to set Access-Control-Allow-Origin for NON OPTIONS requests - this example always sets all headers for a smaller code snippet.

  • In postHere.php setup the following: switch ($_SERVER['HTTP_ORIGIN']) { case 'http://from.com': case 'https://from.com': header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']); header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); header('Access-Control-Max-Age: 1000'); header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With'); break; } This allows your script to make cross domain POST, GET and OPTIONS. This will become clear as you continue to read...
  • Setup your cross domain POST from JS (jQuery example): $.ajax({ type: 'POST', url: 'https://to.com/postHere.php', crossDomain: true, data: '{"some":"json"}', dataType: 'json', success: function(responseData, textStatus, jqXHR) { var value = responseData.someKey; }, error: function (responseData, textStatus, errorThrown) { alert('POST failed.'); } });

When you do the POST in step 2, your browser will send a "OPTIONS" method to the server. This is a "sniff" by the browser to see if the server is cool with you POSTing to it. The server responds with an "Access-Control-Allow-Origin" telling the browser its OK to POST|GET|ORIGIN if request originated from "http://from.com" or "https://from.com". Since the server is OK with it, the browser will make a 2nd request (this time a POST). It is good practice to have your client set the content type it is sending - so you'll need to allow that as well.

MDN has a great write-up about HTTP access control, that goes into detail of how the entire flow works. According to their docs, it should "work in browsers that support cross-site XMLHttpRequest". This is a bit misleading however, as I THINK only modern browsers allow cross domain POST. I have only verified this works with safari,chrome,FF 3.6.

Keep in mind the following if you do this:

  • You will have to think about the security implications. Be careful before doing something like 'Access-Control-Allow-Origin: *'
  • There is a pretty big bug in FF < 3.6 where if the server returns a non 400 response code AND there is a response body (validation errors for example), FF 3.6 wont get the response body. This is a huge pain in the ass, since you cant use good REST practices. See bug here (its filed under jQuery, but my guess is its a FF bug - seems to be fixed in FF4).
  • Always return the headers above, not just on OPTION requests. FF needs it in the response from the POST.

Can it return html for example? I need to return html and something is not working...

Yea you should be able to. Never tried it tho. Your server returning 200? Also is your server returning the headers on the OPTIONs AND POST requests? I have updated my answer with more detail about this. Make sure that your server is responding with the correct content-type header too (like text/html). My recomendation is to use google chrome, right click page>inspect element. Click on network tab, and watch the POST and the response. Should give you info on what is going wrong.

I have tried this, but still get 400 Bad Request on OPTIONS request. and in firefox the second request of POST is never made. :(

Is there a way to callout your local machine in your case statement above? Or do you just have to use the * in this case for the allow origins.

@toddv You can add a domain in Access-Control-Allow-Origin, but downside is you cant add a list of domains.

ajax - How do I send a cross-domain POST request via JavaScript? - Sta...

javascript ajax cross-domain
Rectangle 27 350

Update: Before continuing everyone should read and understand the html5rocks tutorial on CORS. It is easy to understand and very clear.

If you control the server being POSTed, simply leverage the "Cross-Origin Resource Sharing standard" by setting response headers on the server. This answer is discussed in other answers in this thread, but not very clearly in my opinion.

In short here is how you accomplish the cross domain POST from from.com/1.html to to.com/postHere.php (using PHP as an example). Note: you only need to set Access-Control-Allow-Origin for NON OPTIONS requests - this example always sets all headers for a smaller code snippet.

  • In postHere.php setup the following: switch ($_SERVER['HTTP_ORIGIN']) { case 'http://from.com': case 'https://from.com': header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']); header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); header('Access-Control-Max-Age: 1000'); header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With'); break; } This allows your script to make cross domain POST, GET and OPTIONS. This will become clear as you continue to read...
  • Setup your cross domain POST from JS (jQuery example): $.ajax({ type: 'POST', url: 'https://to.com/postHere.php', crossDomain: true, data: '{"some":"json"}', dataType: 'json', success: function(responseData, textStatus, jqXHR) { var value = responseData.someKey; }, error: function (responseData, textStatus, errorThrown) { alert('POST failed.'); } });

When you do the POST in step 2, your browser will send a "OPTIONS" method to the server. This is a "sniff" by the browser to see if the server is cool with you POSTing to it. The server responds with an "Access-Control-Allow-Origin" telling the browser its OK to POST|GET|ORIGIN if request originated from "http://from.com" or "https://from.com". Since the server is OK with it, the browser will make a 2nd request (this time a POST). It is good practice to have your client set the content type it is sending - so you'll need to allow that as well.

MDN has a great write-up about HTTP access control, that goes into detail of how the entire flow works. According to their docs, it should "work in browsers that support cross-site XMLHttpRequest". This is a bit misleading however, as I THINK only modern browsers allow cross domain POST. I have only verified this works with safari,chrome,FF 3.6.

Keep in mind the following if you do this:

  • You will have to think about the security implications. Be careful before doing something like 'Access-Control-Allow-Origin: *'
  • There is a pretty big bug in FF < 3.6 where if the server returns a non 400 response code AND there is a response body (validation errors for example), FF 3.6 wont get the response body. This is a huge pain in the ass, since you cant use good REST practices. See bug here (its filed under jQuery, but my guess is its a FF bug - seems to be fixed in FF4).
  • Always return the headers above, not just on OPTION requests. FF needs it in the response from the POST.

Can it return html for example? I need to return html and something is not working...

Yea you should be able to. Never tried it tho. Your server returning 200? Also is your server returning the headers on the OPTIONs AND POST requests? I have updated my answer with more detail about this. Make sure that your server is responding with the correct content-type header too (like text/html). My recomendation is to use google chrome, right click page>inspect element. Click on network tab, and watch the POST and the response. Should give you info on what is going wrong.

I have tried this, but still get 400 Bad Request on OPTIONS request. and in firefox the second request of POST is never made. :(

Is there a way to callout your local machine in your case statement above? Or do you just have to use the * in this case for the allow origins.

@toddv You can add a domain in Access-Control-Allow-Origin, but downside is you cant add a list of domains.

ajax - How do I send a cross-domain POST request via JavaScript? - Sta...

javascript ajax cross-domain
Rectangle 27 17

Give each xhr request a unique id and store the object reference in an object before sending. Delete the reference after an xhr request completes.

$.ajaxQ.abortAll();

Returns the unique ids of canceled request. Only for testing purposes.

$.ajaxQ = (function(){
  var id = 0, Q = {};

  $(document).ajaxSend(function(e, jqx){
    jqx._id = ++id;
    Q[jqx._id] = jqx;
  });
  $(document).ajaxComplete(function(e, jqx){
    delete Q[jqx._id];
  });

  return {
    abortAll: function(){
      var r = [];
      $.each(Q, function(i, jqx){
        r.push(jqx._id);
        jqx.abort();
      });
      return r;
    }
  };

})();

Returns an object with single function which can be used to add more functionality when required.

Stop all active ajax requests in jQuery - Stack Overflow

jquery ajax
Rectangle 27 17

Give each xhr request a unique id and store the object reference in an object before sending. Delete the reference after an xhr request completes.

$.ajaxQ.abortAll();

Returns the unique ids of canceled request. Only for testing purposes.

$.ajaxQ = (function(){
  var id = 0, Q = {};

  $(document).ajaxSend(function(e, jqx){
    jqx._id = ++id;
    Q[jqx._id] = jqx;
  });
  $(document).ajaxComplete(function(e, jqx){
    delete Q[jqx._id];
  });

  return {
    abortAll: function(){
      var r = [];
      $.each(Q, function(i, jqx){
        r.push(jqx._id);
        jqx.abort();
      });
      return r;
    }
  };

})();

Returns an object with single function which can be used to add more functionality when required.

Stop all active ajax requests in jQuery - Stack Overflow

jquery ajax