Contentful api - getting images - javascript

I am new to contentful API but so far getting content from the API has been pretty straight forward. I have created a new space using their "blog" template and I see that in the "body" field there is an "insert media" button. I don't think I get how this is supposed to be used. When I insert an image into the "body" field, it generates a code that doesn't get rendered when I pull the content form the API. I am using a markdown parser to render the text. If you create an entry with images, these images will be available as an asset. Do I need to make a separate API call for every asset I want rendered with my entry?

When you use the Insert Media button, it should generate something such as:
![Lewis Carroll](//images.contentful.com/zz2okzf5k4px/2ReMHJhXoAcy4AyamgsgwQ/ec4998388330a939288c04558c57477a/lewis-carroll-1.jpg)
That url points to the image directly, so you don't need to do any extra calls to get the asset. The asset is an entity which contains metadata as well as the url to the asset file itself, but in this case you already have that url.
You said you are rendering the Markdown, maybe there's a problem in the code that gets generated? Could you post that?

Related

How do I allow users to create new pages and save them serverside?

I am currently building a personal wiki and I'm having difficulty in creating new article pages without coding them or pasting a blank template.
I've tried DOMImplementation but it's not quite what I'm after, I just want to be able to press a button and a new page is created serverside which I can write in and save, I've looked around on the internet a lot but I keep going in circles.
If anyone can point me in the right direction or show me how that would be awesome.
I also have another problem of including a link to the new file on the index but I could try to sort that out on my own.
Any help is greatly appreciated, thanks.
What you are looking for is probably dynamic pages.The general idea is:
You provide an interface on the frontend for the user to type in the content of the page.
When the content is submitted to the backend, it is saved to a database and it's id is returned for someone to visit the page later on.
On the backend, you create a new endpoint that expects an id, with said id, you query the database and get the contents saved previously.
This fetched content is used to populate a html template and this html is returned as the response to the frontend

How to display BaseX get results in http website?

I have create a BaseX database and a mytest.xq file containing an XQuery for that database. When I write in my browser the following:
localhost:8984/rest?run=mytest.xq
I get the desired results in an xml form. However I want to perform this using the html language to display those results in a website. Is this possible? If it is, can the results from xml be visualized better for example a table?
I have looked all the documentation regarding baseX http and have not found a way
You can add &method=html to your url like so:
localhost:8984/rest?run=mytest.xq&method=html
As long as you are returning html from your query then it will render with the method argument. You don't need RESTXQ for your simple needs.
The main BaseX page has an example file in the webapp folder called restxq.xqm where you can see how the basic home page is set up. You don't need RESTXQ but you can use the header information from that file in your test query and render your page with that in mind.
Also there is an entire app in the webapp/dba folder that is written entirely in RESTXQ.

Putting XML data into Google Sheets

I am trying to create a "database" of sorts in Google Sheets/Fusion Tables using XML files found online (giving information on bus routes and bus stops) so that I can eventually work with it in a Javascript program. The bus stops are identified by a stop_id and the routes by a route tag. I would like to connect these two in the database so that, given a stop_id I will know the route tag of the route the stop is on. Each stop contains the stop_id and latitude/longitude information. The routes contain lists of the stops (stop_id) in them. To create this database, I would like a Google spreadsheet with the route tag as the column head and the list of stop IDs filling the corresponding cells below. In total there are 30 routes. The list of route tags is found on http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=chapel-hill and the route information for route A (for example) is found on http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=chapel-hill&r=A. I have tried using the importxml("routesURL", "/body/route//stop[stopId]") command in Google sheets, but it returns a parse error or that "Imported content is empty" when I do so. To reiterate my goal, I would like to know for a specific stop_id what the route tag is. Any tips? Is my strategy all wrong? Thank you very much!
It's not a typical xml, ie no real values inside the opening tag and closing tag. This is a typical xml file, from which importxml can extract Tove, Jani, Reminder, etc. Just Try:
=importxml("https://www.w3schools.com/xml/note.xml", "*")
But, in case of your first file, there's no values to extract.
I have searched and found they also provide JSON format. Just replace XML to JSON in the URL like this:
http://webservices.nextbus.com/service/publicJSONFeed?command=routeConfig&a=chapel-hill&r=A
GS doesn't provide a built-in function for JSON, but you can import JSON via GAS.

Load a js image

I am writing an Android RSS reader. It uses webview to display each article. To identify the most read news I load a fake image from my server like:
<img src='http://example.com/update_read_count.php?article_id=1234'>
At the server I will increment the view counter.
Is there a way I can respond back with the number of times it has been read. Or is there a way I can update a DOM element inject this count in the page?
Since it is an image, the only way you could do that would be to generate an image containing the text you wanted on the server. e.g. with this function.
If you wanted to update the DOM, you'd need to get the data in a more useful format. Using XMLHttpRequest to fetch the data and then returning it in JSON format would be traditional.

Embed PDF w/POST Parameters

I need to pass a bunch of parameters to a web API method which returns a byte array that is needed to display a PDF report on a page. I tried doing this via a GET method but the query string is too long and it throws an exception. I have tried shortening the query string as much as physically possibly but it is still too long. All of the external PDF viewer libraries require a string path to the PDF. I need something that allows me to supply a string path and some parameters or just a string path but that uses a POST instead of a GET. I was thinking about POSTing the object values to the web API along with a GUID and storing it somehow on the server then with an tag calling another API method and retrieving the stored data by only passing the GUID as a parameter but I don't want to make a database table for only that to save and retrieve the data. I entertained the idea of saving the byte array to a PDF on the server and then simply pointing a PDF viewer to that after but I had no success with that either and that is not ideal.
File.WriteAllBytes(path, res);
The code above is the most common way I have found to save the file but when I open the new PDF file after it says it is corrupt and it won't allow me to open it. Ideally I would just find a method that allows me to POST data to my web API then embeds the result in a pdf.js/pdfObject/ control. Please help, this has stumped me for days. Thanks in advance!!

Categories

Resources