0

I am trying to create gists in Github via REST ASSURED.

To create a gist a need to pass file names and their contents.

Now, the content of the file is something which is being rejected by the API.

Example:

{
    "description": "Hello World Examples",
    "public": true,
    "files": {
        "hello_world.rb": {
            "content": "class HelloWorld\n def initialize(name)\n @name = name.capitalize\n end\n def sayHi\n puts \"Hello !\"\n end\nend\n\nhello = HelloWorld.new(\"World\")\nhello.sayHi"
        },
        "hello_world.py": {
            "content": "class HelloWorld:\n\n def init(self, name):\n self.name = name.capitalize()\n \n def sayHi(self):\n print \"Hello \" + self.name + \"!\"\n\nhello = HelloWorld(\"world\")\nhello.sayHi()"
        },
        "hello_world_ruby.txt": {
            "content": "Run ruby hello_world.rb to print Hello World"
        },
        "hello_world_python.txt": {
            "content": "Run python hello_world.py to print Hello World"
        }
    }

This is how the the API wants the JSON to be, I could get this via my code:

{
  "description": "Happy World",
  "public": true,
  "files": {
    "sid.java": {
      "content": "Ce4z5e22ta"
     },
    "siddharth.py": {
      "content": "def a:
    if sidh>kundu:
        sid==kundu
    else:
        kundu==sid

       "
     }
  }
}

So the change in the indentations is causing GitHUb API to fail this with 400 error. Can someone please help?

3
  • 1
    pretty sure JSON does not allow multiline strings stackoverflow.com/a/2392888/7927820 Commented Nov 23, 2018 at 14:28
  • It seems you're generating JSON by concatenating string parts. Don't do that. Use an actual JSON library to generate JSON nodes, or even better, to map objects to JSON. The JSON library will generate valid JSON by escaping everything that needs to be escaped.
    – JB Nizet
    Commented Nov 23, 2018 at 14:29
  • You are missing one final closing bracket in your first code snippet -- is that intentional? Because that's not a valid JSON text. Commented Nov 23, 2018 at 14:38

2 Answers 2

1

As pointed out in the comments, JSON does not allow control characters in strings. In the case of line breaks, these were encoded as \n in the example.

You should definitely consider using a proper library to create the JSON rather than handling the raw strings yourself.

0
  1. Create a POJO which will represent your gist (i.e. object with fields like 'description', 'files' collection. And separate POJO for file containing string fields 'name' and 'content';
  2. Do something like this to convert your gist:

    try {
        GistFile file new GistFile();// Assuming this is POJO for your file
        //Set name and content
        Gist gist = new Gist(); //Asuming this is a POJO for your gist
        gist.addFile(file);
        //Add more files if needed and set other properties
        ObjectMapper mapper = new ObjectMapper();
        String content = mapper.writeValueAsString(gist);
        //Now you have valid JSON string
    } catch (Exception e) {
        e.printStackTrace();
    }
    

This is for com.fasterxml.jackson.databind.ObjectMapper or use different JSON library

  1. Actually there are GitHub specific libraries which do most of the job for you. Please refer to this question: How to connect to github using Java Program it might be helpful

Not the answer you're looking for? Browse other questions tagged or ask your own question.