400 Bad Request: Translate CURL to jquery.ajax() #119

Closed
landwire opened this Issue Mar 17, 2014 · 1 comment

Projects

None yet

2 participants

@landwire

Sorry to post this as a new issue, but I am totally stuck with authorisation via an ajax call. I posted this under another issue a month ago, but never got a reply.

I am making this curl call and it works great (NO ERRORS) now after installing the WP BASIC Auth as mentioned in this ticket #66. That info could really go into the Basic Usage documentation!

curl --data '{"title": "Hello Updated new post with cats!"}'     -H "Content-Type: application/javascript"     --user username:password     http://apitest.landwire.net/wordpress/wp-json.php/posts/1

How can I write that curl as jquery ajax? I tried all sorts of variations of the below:

// extra function
function setAuthHeader(xhr){
        var creds = username + ':' + password;
        var basicScheme = btoa(creds);
        var hashStr = "Basic "+basicScheme;
        xhr.setRequestHeader('Authorization', hashStr);
}
// NOT works
function editPost(id) {
if(id) {    
var the_url = 'http://apitest.landwire.net/wordpress/wp-json.php/posts/'+ id;
}
else {
   return;
    }
    jQuery.ajax({
  url: the_url,
 //headers: {
   //'Authorization': "Basic " + btoa("username" + ":" + "password"),
   //  'Content-Type': 'application/javascript'
  //},
//headers: {
  //     "HTTP:Authorization": "Basic " + btoa("username:password")
    //},
//username: 'apitest',
  //   password: 'password',
//beforeSend: function(xhr) { //xhr.setRequestHeader("Authorization", "Basic " + btoa("username" + ":" + "password")); },
  //  beforeSend: function(xhr) { 
  //  xhr.setRequestHeader("Authorization", "Basic " + //btoa("username:password")); 
  //  },
beforeSend: function(xhr) { xhr.setRequestHeader('Authorization', hashStr);
},
   data: {
      'title': 'Hello Updated MANAN World!',
      //'username': 'username',
      //'password': 'password'
   },
  dataType: 'json',
  'Content-Type': 'application/json', //also tried application/javascript
  type: 'PUT',
success: function(data, textStatus, jqXHR){
console.log('post creation success! ' + textStatus);
    var json_response = data;
    console.log('Updated Post ID ' + id + ': ', data);
   },
        error: function(jqXHR, textStatus, errorThrown){
            console.log('error creating post, ' + textStatus + ', ' + errorThrown);
        }
});
}
@rmccue
Member
rmccue commented Apr 6, 2014

The above cURL call:

curl --data '{"title": "Hello Updated new post with cats!"}'     -H "Content-Type: application/javascript"     --user username:password     http://apitest.landwire.net/wordpress/wp-json.php/posts/1

Maps to the following jQuery.ajax call:

jQuery.ajax({
    data: JSON.stringify( {
        title: "Hello Updated new post with cats!"
    } ),
    type: "PUT",
    username: "username",
    password: "password",
    url: "http://.../posts/1",
});

In general, you should use the provided JS library, wp-api.js.

Thanks for the feedback!

@rmccue rmccue closed this Apr 6, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment