Overwriting website page HTML using a script

Highlighted
New Member

I want to upload HTML from a file on my local machine to a page and publish the page. With Ruby I am using the following:

 

upload_url = "https://api.hubapi.com/content/api/v2/pages/#{file_id}?hapikey=#{ENV["HUBSPOT_API_KEY"]}"
 
Reading a file and then setting the payload as follows:
 
payload = JSON.generate({
      "widgets": {
        "right_column": {
            "body": {
                "html"file_contents
            }
        }
      }
    })
 
Then using RestClient in this way:
 
response = RestClient.put(upload_urlpayload:content_type => :json:accept => :json)
 
Whenever I check the page it has overwritten the HTML and it is a blank page.
 
The documentation also says to publish changes I should use the following but it is also causing an error:
 
publish_url = "https://api.hubapi.com/content/api/v2/pages/#{file_id}/publish-action?hapikey=#{ENV["HUBSPOT_API_KEY"]}"
payload = JSON.generate({"action""push-buffer-live"})
response = RestClient.post(publish_urlpayload:content_type => :json:accept => :json)
 
The responses from these requests do not highlight the error very clearly. Please can somebody offer some suggestions?
Reply
0 Upvotes
1 Reply 1
HubSpot Moderator

Hi @alex_getmosh,

 

Are you still having trouble with getting this to work? And if you are, could you link me to your page? I think the best way to get this working is to first pull down the JSON for the page using this endpoint: https://developers.hubspot.com/docs/methods/pages/get_pages_page_id. Then you can copy the structure in the JSON, replace the specific content, and push it back up. One thing that could be happening in your case is that you're trying to update content inside the widgets field, when actually that may need to be done inside the widget_containers field. widget_containers is where the module data go when they're nested inside flexible columns for example. So if you're trying to pass in your module data inside the widgets field when it actually needs to be in the widget_containers field, it's possible that the tool just isn't reading it correctly and sets the content to be blank.

 

Here's an example from my own testing. I made a GET request for a page and had this JSON inside the widget_containers field:

 

"widget_containers": {
  "module_1505760340809103": {
    "widgets": [
      {
        "body": {
          "html": "<p>original html</p>"
        },
        "child_css": {},
        "css": {},
        "id": 1570633869444,
        "module_id": 2564054,
        "order": 0,
        "type": "module"
      }
    ]
  }
}

The widgets field was blank. I then copied that and pasted it in as the JSON for my request body to content/api/v2/pages/Smiley Tongueage_id. Then I edited the content slightly and pushed this up with a PUT request:

 

{
  "widget_containers": {
    "module_1505760340809103": {
      "widgets": [
        {
          "body": {
            "html": "<p>new html</p>"
          },
          "child_css": {},
          "css": {},
          "id": 1570633869444,
          "module_id": 2564054,
          "order": 0,
          "type": "module"
        }
      ]
    }
  },
  "publish_immediately": true
}

That updates the content "buffer", which is basically the saved/unpublished version of the page. I also set the publish_immediately value to true. And then when I make a POST request to the endpoint documented here: https://developers.hubspot.com/docs/methods/pages/post_pages_page_id_publish_action, and passing in JSON of:

 

{"action":"schedule-publish"}

...my page gets published with the new content.

 

I don't know if that helps in your specific case, but if you're still having any trouble with it, feel free to follow up here and link me to a page so I can take a look at what the module data look like.

 

Leland Scanlan

HubSpot Developer Support
Reply
0 Upvotes