Cant' execute PUT to update post from JS


#1

I’m unable to execute a PUT from js although the GET for the same url works just fine.

I’m doing exactly as told here but I’m unable to update the post.

Request URL:http://info.localytics.com/content/api/v2/blog-posts/4251860569?hapikey=XXXXXXXXXXXXXXX
Request Method:PUT
Status Code:501 Not Implemented

Any ideas of what might be blocking this?


#2

Hi @MigMac,

The domain of your request should be api.hubapi.com, not your domain (info.localytics.com). If you change that out it should work. Let me know if you still have troubles.

-Zack


#3

Thanks Zack, I’ve tried that here is how (hoping it may give you more context info):

jQuery.ajax({
type: ‘PUT’,
dataType: ‘json’,
url: “https://api.hubapi.com/content/api/v2/blog-posts/4251860569?hapikey=XXXXXX”,
data: {
widgets: JSON.stringify( widgets_data )
}
}).done(function(data){
window.the_post_data = data;
console.log(data);
}).fail(function( jqXHR, textStatus ) {
window.tmp_jqxhr = jqXHR;
console.log( "Request failed: " + textStatus );
});

Where I basically pull the widget_data, update a field value and want to save (PUT).

And this is what I get:

Request URL:https://api.hubapi.com/content/api/v2/blog-posts/4251860569?hapikey=XXXXXXXXX
Request Method:OPTIONS
Status Code:405 Method Not Allowed

Still unclear why it’s interpreting a OPTIONS method instead.

Also do you know how is the cross-domain restriction supposed to work in this case?,
Should I use jsonp or is this supposed to be used

We want to save some custom data for each post and that’s why we end up using the widget. any other thoughts to do this?


#4

Hi @MigMac,

Sorry I didn’t read the title to of the post before I responded. HubSpot’s APIs do not allow cross origin requests (except for the javascript events API).

Where is this code living? Can you give me an idea of what you are trying to do? Right now it looks like you are trying to update a blog post based on what someone is doing on another webpage. You will likely have to proxy the request through your own server which allows CORS, however an example may reveal another work around.

-Zack


#5

We would like to add a counter per blog post (in HS blog) that counts a specific action performed by the user, we ideally want to save that count in HS and that’s why we ended up using a widget to save this <field,value> on a per post basis.
Any other alternative we might use?


#6

You could keep rewriting a module in the post, but you will end up having to make a lot of calls and keeping track of things on your end. Instead I would write something that you can simply embed into a blog post that when it loads it looks up into your database how many “actions” have been performed on that post. You could use HubL to get the internal ID of the post so you don’t have to rely on the URL of the post, which could change.

-Zack