APIs & Integrations

willsteward
Member | Gold Partner
Member | Gold Partner

COS Blog Post API - Invalid Json error creating a new post?

SOLVE

I’ve been trying to import ~130 blog posts via the API: I was given all of these in static HTML format, and then converted it into JSON which I believe to be syntactically valid, as far as I can see.

Here is the JSON I’m sending in the body of the request, as a test containing 1 single post: http://www.jsoneditoronline.org/?id=8dcc176cf11ce11495c4d6611dcc1544

When POST (to https://api.hubapi.com/content/api/v2/blog-posts), I get the following response from the API:

{"message": "Invalid syntax for the json data", "errors": [], "succeeded": false}  

Can anyone see an obvious problem here? Slowly driving me insane. As far as I can see, the fields all match up, and everything is properly escaped: any ideas?

This is the only thing we’re trying to do via the API, so if anyone at HubSpot has access to the request logs, it’ll be the only requests coming from this portal (presumably identifiable from the content_group_id).

Thanks for your help!

0 Upvotes
1 Accepted solution
willsteward
Solution
Member | Gold Partner
Member | Gold Partner

COS Blog Post API - Invalid Json error creating a new post?

SOLVE

Thanks for your help Peter: gave me the nudge needed to look beyond the JSON body itself, and see the error I was making.

I’m using NodeJS & initially the request NPM module ( link ) to manage the request, and had the following code (where the variable “post” contains the JSON object string shared above)… Highlighted my mistake with ** (embarrassingly… :blush: ) :

var request = require('request');
var failedPosts = [];

var savePost = function(post) {
  var apiEndpoint = "https://api.hubapi.com/content/api/v2/blog-posts?hapikey=REDACTED";
  **var options = {**
    url: apiEndpoint,
    json: true,
    body: post
  };

  **request.post(apiEndpoint, post, function(err, response, body) {**
    if (response.statusCode != 201) {
      console.log("Failed to save: " + post.name);
      console.log("----body dump-----------");
      console.log(body);
      console.log("----response dump----");
      console.log(response);
      console.log("-------------------------------");
      failedPosts.push(post.name); 
      return false;
    }
    return true;
  });
};

The error message HubSpot was returning in the response then led me to focus on the JSON object I was sending within the request, and as a result I didn’t even think it could be something else.

I re-wrote the block of code from scratch using the needle NPM library instead (which I’ve not used before, so forced me to cross reference the code), and realised that I wasn’t passing the options variable into the request.post() function first time round.

Initially I wasn’t using the options variable, and did have malformed json (didn’t pass validation checks, the import file I was reading from used single quotes, rather than double, to define properties) which is how this happened. I read in another thread here that single could cause character escaping issues, so completely focused on the request body itself, rather than looking at the code as a whole.

I’m assuming that the error I was getting was because the headers weren’t set correctly to application/json? If that’s it, it would be great to have either something added to the docs explaining what could cause this error message, or alternatively have a separate error message for each?

Thought the feedback there could help: ironically if I had no error message at all (just knew that it failed), I probably would have spotted it quickly!

Here’s the final working code, for reference, if it helps in any way:

  var failedPosts = [];
  var savePost = function(post) {
  var apiEndpoint = "https://api.hubapi.com/content/api/v2/blog-posts?hapikey=**redacted**";
  writeToLog("saved-posts.json", "\n" + post + " \n");
  var options = {
    headers: {
      "Content-Type": "application/json"
    }
  };

  needle.post(apiEndpoint, post, options, function(err, response) {
    if (response.statusCode != 201) {
      console.log("Failed to import: " + post.name);
      console.log("----response---------");
      console.log(response);
      console.log("----end response-----");
      failedPosts.push(post.name);
      return false;
    }
    console.log("Successfully imported:" + post.name);
    return true;

  });
}; 

Thanks a million. A good lesson learned for me too about making assumptions!

When I’ve tied this migration project up and have some time, I’ll tidy up the code and publish it to Github: could be a useful tool for people trying to migrate any blog to the HubSpot COS. I used the free apifier.com API to scrape all the blog posts from the site (using jQuery selectors to find the relevant properties on each page), converted the output into the JSON HubSpot expects, then looped through those (with a brief wait between each request to prevent breaking the request limit thresholds).

Could even be an interesting way to approach an additional HubSpot blog import tool, that encourages more people with well established (non-Wordpress) blogs to get that content into HubSpot, rather than just installing the tracking code :slight_smile:

View solution in original post

0 Upvotes
4 Replies 4
willsteward
Member | Gold Partner
Member | Gold Partner

COS Blog Post API - Invalid Json error creating a new post?

SOLVE

Anyone able to help? Support can’t help us with this, and we need to get the entire blog migrated this week to stop the migration project going off track: otherwise it’s going to delay our entire project, and our planned campaigns.

Thanks for your help.

0 Upvotes
3PETE
HubSpot Employee
HubSpot Employee

COS Blog Post API - Invalid Json error creating a new post?

SOLVE

@willsteward I was not able to replicate your issue. I made the call in postman and copied your json block in I got back a successful 201. How are you authenticating in? Hapikey or oauth2?

{
“analytics_page_id”: “4804571200”,
“archived”: false,
“attached_stylesheets”: [],
“author_user_id”: null,
“blog_author”: {},
“blog_author_id”: null,
“blueprint_type_id”: 0,
“campaign”: null,
“campaign_name”: null,
“category_id”: 3,
“cloned_from”: null,
“comment_count”: 0,
“compose_body”: null,
“content_group_id”: 4544204069,
“created”: 1486043708000,
“css”: {},
“css_text”: “”,
“deleted_at”: 0,
“deleted_by”: null,
“domain”: “”,
“enable_domain_stylesheets”: true,
“enable_layout_stylesheets”: true,
“featured_image”: “https://belighted-website.s3.amazonaws.com/uploads/desk-notebook-office-macbook.jpg”,
“featured_image_alt_text”: “”,
“flex_areas”: {},
“folder_id”: null,
“footer_html”: “”,
“freeze_date”: 1486043708000,
“head_html”: “”,
“html_title”: “My experience working remotely at Belighted”,
“id”: 4804571200,
“include_default_custom_css”: null,
“is_draft”: true,
“is_instant_email_enabled”: false,
“is_social_publishing_enabled”: false,
“keywords”: [],
“language”: null,
“last_edit_session_id”: null,
“last_edit_update_id”: null,
“legacy_blog_tabid”: null,
“meta_description”: “After two years of trying out remote work, I thought it was time to share my feelings about it. I’ll try to point out some tips, benefits and disadvantages about remote work. I hope you’ll enjoy reading this article.”,
“meta_keywords”: null,
“name”: “My experience working remotely at Belighted”,
“page_expiry_date”: null,
“page_expiry_enabled”: null,
“page_expiry_redirect_id”: null,
“page_expiry_redirect_url”: null,
“page_redirected”: null,
“performable_url”: null,
“personas”: [],
“portal_id”: 2323210,
“post_body”: “

Introduction

\n\n

I have been working at Belighted for 2 years and 3 months now. It all started with an internship during my last year of studies. When I joined the team of 11 people, working remotely wasn’t something really pushed forward, but after a year the possibility to work remotely one day per week was introduced. I took that opportunity to try it out. After a year, the experience was quite positive for the whole team, and so the managing team decided to remove the limit of one day a week of working remotely. Since then I’m used to work 3 days at home. I’ll try to point out some tips, benefits and disadvantages about remote work.

\n\n

First remote work experience

\n\n

When I tried to work from home for the first time I was really excited, something completely new for me, a new world to discover. But with time some bad habits appeared: starting the day in pajamas, take time to respond to non-work related emails, phone calls, etc. When I realised that my productivity was going down, I decided to change things that weren’t related to work. I have read some books and blog posts about remote working which opened my eyes about my mistakes and what I could do better in order to be more productive.

\n\n

Things I learned

\n\n

The first habit I changed, was to stop working in pajamas. You could say this isn’t really a big change, but working in pajamas made me unconsciously work less concentrated. I still wear casual clothes, because being at ease helps me work in a better way.

\n\n

Secondly I started to take real breaks. Like going downstairs, preparing a coffee, taking 5 minutes enjoying drinking it, instead of taking it directly back to my computer. It seems also a logical idea, and it helps you taking a break off the screens, and that can be important for a developer.

\n\n

Thirdly I started being more focused on work, because I decided to stop answering emails, phone calls that weren’t work-related, even if it took me 1 minute to deal with it (interesting reading <a href=“http://lifehacker.com/how-long-it-takes-to-get-back-on-track-after-a-distract-1720708353”>about this point here). Now, I rather take 5 good minutes and process non work related stuff. Therefore I turn notifications of non-work related applications off on my phone and computer. What I started to do, is that every time I think about doing something that could be a distraction, like reading an article, replying to a non-urgent email, doing the dishes, I write it down on a to-do list of things you shouldn’t do and save them for later.

\n\n

Fourthly, I understood that when working from home, it’s really vital to setup a workspace. When your home is also your office, it can be whatever you want it to be: coworking, home, etc. But wherever you want to work from, you need it to be a place of work, that is very important. At home, it helps you to differentiate home and work, and lets your brain know when it’s time to turn work mode on or off.

\n\n

Lastly, I changed the way I communicate with my colleagues. At Belighted our main tool for communication is <a href=“https://slack.com/”>Slack, where you can easily call a collegue instead of chatting with him in order to discuss an issue you may face. In the beginning I tried to find a solution on my own, instead of using the knowledge of my colleagues who might already have dealt with a similar problem. We now frequently use a channel on our Slack, named #dev where everyone can ask a developer-related question.

\n\n

But never forget that everyone procrastinates, whether they are in an office with everyone else, or working remotely, sometimes you’re just not 100% focused and productive.

\n\n

Social life

\n\n

Working from home, or going to the office, has one major difference in social life. Many friends ask me: “Isn’t it boring being home alone, instead of going to the office and seeing people?”. For sure you have less human contact, but through Slack, we are able to discuss with everyone about stupid things of life.\nWhen you’re in an office, most conversation involves talking about nothing, such as sports, movies, funny stories, anecdotes, anything but work. This is just a normal part of human behaviour. But when you are working remotely, you have the feeling that you shouldn’t bother your colleagues with communication that isn’t work-related and vital.\nAbout communication, video calls are also important, you shouldn’t just type, talk. Sometimes a quick face-to-face chat helps to get ideas across quickly.

\n\n

Some benefits of working from home

\n\n

My first point will be car-related. All the stress that a ride with a car can bring is erased, such as having a flat tire, an accident, traffic jams, etc. Furthermore, when you don’t have a company car nor a fuel card, you can spare a certain amount of money.\nSecond benefit is being able to live a healthier life. Working from home gives me the opportunity to eat more healthy meals, instead of buying sandwiches, junk food, etc. But also being able to exercise more often, as I lose less time travelling from the office to home. Those two points makes me also more productive for work as I’m feeling more comfortable in my skin.\nFinally, As I’m having a little dog at home, I prefer being around him, as I’m feeling guilty leaving him alone a whole day long. I can spend some time for him, such as playing or cuddling him. And that also helps you change your mind a bit.

\n\n

Negative aspects of working from home

\n\n

One of the biggest points is for me: sociability. Being the whole day at home, and seeing no one is sometimes really difficult, but as I explained earlier, good communication with my colleagues about little things of life, makes this point less hard to live with. Also, when going to the office, you see other people walking around in the streets, a different environment. But when you arrive at work, it’s actually the same than at home as you are behind your desk, working on your projects.\nAnother negative point, is maybe particular for me, because my internet connection at home is quite bad, but I guess I’m not the only one in this case. Therefore a bad infrastructure can be very negative on your productivity.

\n\n

Conclusion

\n\n

In a more general way, I loved working remotely, and will continue doing it. I will certainly continue learning about the way of working remotely more efficiently. It has negative and positive aspects, but what I try to do is to mix both environments, and get the maximum out of each one.

\n<div id=“disqus_thread”><iframe id=“dsq-app1” name=“dsq-app1” allowtransparency=“true” frameborder=“0” scrolling=“no” tabindex=“0” title=“Disqus” width=“100%” src=“https://disqus.com/embed/comments/?base=default&version=105ec7c1b6b6b3960e72ac39bf0bd1c5&f=belighted...” style=“width: 1px !important; min-width: 100% !important; border: none !important; overflow: hidden !important; height: 0px !important;”>”,
“post_summary”: “

Introduction

\n

I have been working at Belighted for 2 years and 3 months now. It all started with an internship during my last year of studies. When I joined the team of 11 people, working remotely wasn’t something really pushed forward, but after a year the possibility to work remotely one day per week was introduced. I took that opportunity to try it out. After a year, the experience was quite positive for the whole team, and so the managing team decided to remove the limit of one day a week of working remotely. Since then I’m used to work 3 days at home. I’ll try to point out some tips, benefits and disadvantages about remote work.

”,
“preview_image_src”: null,
“preview_key”: “L7pFvc2_”,
“processing_status”: “”,
“publish_date”: 1484629200000,
“publish_immediately”: null,
“published_url”: null,
“rss_body”: “”,
“rss_email_author_line_template”: false,
“rss_email_blog_image_max_width”: null,
“rss_email_by_text”: “By”,
“rss_email_click_through_text”: “Read more »”,
“rss_email_comment_text”: “Comment »”,
“rss_email_entry_template”: null,
“rss_email_entry_template_enabled”: false,
“rss_email_image_max_width”: 0,
“rss_summary”: “”,
“scheduled_update_date”: null,
“slug”: “my-experience-working-remotely-at-belighted”,
“staged_from”: null,
“state”: “DRAFT”,
“style_override_id”: null,
“subcategory”: “normal_blog_post”,
“tms_id”: null,
“topic_ids”: [],
“translated_from_id”: null,
“unpublished_at”: null,
“updated”: 1486043708000,
“url”: “http://www.cheetahcommute.com/my-experience-working-remotely-at-belighted”,
“use_featured_image”: true,
“widget_containers”: {},
“widgets”: {}
}
willsteward
Solution
Member | Gold Partner
Member | Gold Partner

COS Blog Post API - Invalid Json error creating a new post?

SOLVE

Thanks for your help Peter: gave me the nudge needed to look beyond the JSON body itself, and see the error I was making.

I’m using NodeJS & initially the request NPM module ( link ) to manage the request, and had the following code (where the variable “post” contains the JSON object string shared above)… Highlighted my mistake with ** (embarrassingly… :blush: ) :

var request = require('request');
var failedPosts = [];

var savePost = function(post) {
  var apiEndpoint = "https://api.hubapi.com/content/api/v2/blog-posts?hapikey=REDACTED";
  **var options = {**
    url: apiEndpoint,
    json: true,
    body: post
  };

  **request.post(apiEndpoint, post, function(err, response, body) {**
    if (response.statusCode != 201) {
      console.log("Failed to save: " + post.name);
      console.log("----body dump-----------");
      console.log(body);
      console.log("----response dump----");
      console.log(response);
      console.log("-------------------------------");
      failedPosts.push(post.name); 
      return false;
    }
    return true;
  });
};

The error message HubSpot was returning in the response then led me to focus on the JSON object I was sending within the request, and as a result I didn’t even think it could be something else.

I re-wrote the block of code from scratch using the needle NPM library instead (which I’ve not used before, so forced me to cross reference the code), and realised that I wasn’t passing the options variable into the request.post() function first time round.

Initially I wasn’t using the options variable, and did have malformed json (didn’t pass validation checks, the import file I was reading from used single quotes, rather than double, to define properties) which is how this happened. I read in another thread here that single could cause character escaping issues, so completely focused on the request body itself, rather than looking at the code as a whole.

I’m assuming that the error I was getting was because the headers weren’t set correctly to application/json? If that’s it, it would be great to have either something added to the docs explaining what could cause this error message, or alternatively have a separate error message for each?

Thought the feedback there could help: ironically if I had no error message at all (just knew that it failed), I probably would have spotted it quickly!

Here’s the final working code, for reference, if it helps in any way:

  var failedPosts = [];
  var savePost = function(post) {
  var apiEndpoint = "https://api.hubapi.com/content/api/v2/blog-posts?hapikey=**redacted**";
  writeToLog("saved-posts.json", "\n" + post + " \n");
  var options = {
    headers: {
      "Content-Type": "application/json"
    }
  };

  needle.post(apiEndpoint, post, options, function(err, response) {
    if (response.statusCode != 201) {
      console.log("Failed to import: " + post.name);
      console.log("----response---------");
      console.log(response);
      console.log("----end response-----");
      failedPosts.push(post.name);
      return false;
    }
    console.log("Successfully imported:" + post.name);
    return true;

  });
}; 

Thanks a million. A good lesson learned for me too about making assumptions!

When I’ve tied this migration project up and have some time, I’ll tidy up the code and publish it to Github: could be a useful tool for people trying to migrate any blog to the HubSpot COS. I used the free apifier.com API to scrape all the blog posts from the site (using jQuery selectors to find the relevant properties on each page), converted the output into the JSON HubSpot expects, then looped through those (with a brief wait between each request to prevent breaking the request limit thresholds).

Could even be an interesting way to approach an additional HubSpot blog import tool, that encourages more people with well established (non-Wordpress) blogs to get that content into HubSpot, rather than just installing the tracking code :slight_smile:

0 Upvotes
3PETE
HubSpot Employee
HubSpot Employee

COS Blog Post API - Invalid Json error creating a new post?

SOLVE

@willsteward I really appreciate the progress update and the code. Will help future developers perusing the forum!

Let me know if you run into any other issues.