62009030
62009030

Reputation: 347

How to send MarkDown to API

I'm trying to send Some Markdown text to a rest api. Just now I figure it out that break lines are not accepted in json.

Example. How to send this to my api:

An h1 header
============

Paragraphs are separated by a blank line.

2nd paragraph. *Italic*, **bold**, and `monospace`. Itemized lists
look like:

  * this one
  * that one
  * the other one

Note that --- not considering the asterisk --- the actual text
content starts at 4-columns in.

> Block quotes are
> written like so.
>
> They can span multiple paragraphs,
> if you like.

Use 3 dashes for an em-dash. Use 2 dashes for ranges (ex., "it's all
in chapters 12--14"). Three dots ... will be converted to an ellipsis.
Unicode is supported. ☺

as

{
    "body" : " (the markdown) ",
}

Upvotes: 1

Views: 9517

Answers (2)

RohilVisariya
RohilVisariya

Reputation: 46

You need to replace the line-endings with \n and then pass it in your body key.

Also, make sure you escape double-quotes (") by \" else your body will end there.

# An h1 header\n============\n\nParagraphs are separated by a blank line.\n\n2nd paragraph. *Italic*, **bold**, and `monospace`. Itemized lists\nlook like:\n\n  * this one\n  * that one\n  * the other one\n\nNote that --- not considering the asterisk --- the actual text\ncontent starts at 4-columns in.\n\n> Block quotes are\n> written like so.\n>\n> They can span multiple paragraphs,\n> if you like.\n\nUse 3 dashes for an em-dash. Use 2 dashes for ranges (ex., \"it's all\nin chapters 12--14\"). Three dots ... will be converted to an ellipsis.\nUnicode is supported.

Upvotes: 2

Artemis
Artemis

Reputation: 638

As you're trying to send it to a REST API endpoint, I'll assume you're searching for ways to do it using Javascript (since you didn't specify what tech you were using).

Rule of thumb: except if your goal is to re-build a JSON builder, use the ones already existing.

And, guess what, Javascript implements its JSON tools ! (see documentation here)

As it's shown in the documentation, you can use the JSON.stringify function to simply convert an object, like a string to a json-compliant encoded string, that can later be decoded on the server side.

This example illustrates how to do so:

var arr = {
    text: "This is some text"
};
var json_string = JSON.stringify(arr);
// Result is:
// "{"text":"This is some text"}"
// Now the json_string contains a json-compliant encoded string.

You also can decode JSON client-side with javascript using the other JSON.parse() method (see documentation):

var json_string = '{"text":"This is some text"}';
var arr = JSON.parse(json_string);
// Now the arr contains an array containing the value
// "This is some text" accessible with the key "text"

If that doesn't answer your question, please edit it to make it more precise, especially on what tech you're using. I'll edit this answer accordingly

Upvotes: 5

Related Questions