如何将 MarkDown 发送到 API

How to send MarkDown to API

我正在尝试将一些 Markdown 文本发送到休息 api。刚才我发现 json 中不接受换行符。

例子。如何将此发送到我的 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. ☺

作为

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

当你试图将它发送到 REST API 端点时,我假设你正在寻找使用 Javascript 来完成它的方法(因为你没有指定什么您使用的技术)。

经验法则:除非您的目标是 re-build 一个 JSON 构建器,否则请使用已经存在的构建器。

而且,猜猜看,Javascript 实现了它的 JSON 工具! (see documentation here)

the documentation 所示,您可以使用 JSON.stringify 函数简单地将对象(如字符串)转换为 json-compliant 编码字符串,稍后可以在服务器端。

此示例说明了如何执行此操作:

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.

您还可以使用其他 JSON.parse() 方法 (see documentation) 将 JSON client-side 解码为 javascript:

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"

如果这不能回答您的问题,请对其进行编辑以使其更加精确,尤其是在您使用的技术方面。我会相应地编辑这个答案

您需要用 \n 替换行尾,然后将其传递到您的 body 键中。

此外,请确保通过 \" 转义双引号 ("),否则您的正文将在那里结束。

# 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.