javascriptgithub-apigist

Github Gist: New Lines?


I'm trying to create a gist with multiple lines of content but not sure the best way to go about it. \n isn't working and neither is adding two empty lines. It shows up as a single line of text.

var content = 'content on\nnewline here';
$.ajax({ 
    url: 'https://api.github.com/gists',
    type: 'POST',
    beforeSend: function(xhr) { 
        xhr.setRequestHeader("Authorization", "token TOKEN-HERE"); 
    },
    data: '{"description": "a gist for a user with token api call via ajax","public": true,"files": {"file1.txt": {"content": "' + content + '"}}}'
}).done(function(response) {
    console.log(response.id);
});

How do I change the content variable to contain newline characters? I want the output to eventually show up as a .txt so I don't think I can change the media type? Thanks.


Solution

  • This is just a guess, but it might be because you're JSON-ifying manually, and the \n isn't being escaped properly. Try using JSON.stringify:

    JSON.stringify({"description": "a gist for a user with token api call via ajax","public": true,"files": {"file1.txt": {"content": content }}})