Skip to content

Conversation

@webholik
Copy link
Contributor

@webholik webholik commented Apr 12, 2021

Fixes #459
C#:

using RestSharp;
namespace HelloWorldApplication {
  class HelloWorld {
    static void Main(string[] args) {
      var client = new RestClient("http://example.com");
      client.Timeout = -1;
      var request = new RestRequest(Method.GET);
      request.AddHeader("Content-Type", "application/json");
      var body = @"{" + "\n" +
      @"    ""hello"": ""world""," + "\n" +
      @"    ""this"": true," + "\n" +
      @"    ""call"": [1,2,3,null]" + "\n" +
      @"}";
      request.AddParameter("application/json", body,  ParameterType.RequestBody);
      IRestResponse response = client.Execute(request);
      Console.WriteLine(response.Content);
    }
  }
}

Dart:

import 'dart:convert';
import 'package:http/http.dart' as http;

void main() async {
  var headers = {
    'Content-Type': 'application/json'
  };
  var request = http.Request('GET', Uri.parse('http://example.com'));
  request.body = json.encode({
      "hello": "world",
      "this": true,
      "call": [
          1,
          2,
          3,
          null
      ]
  });
  request.headers.addAll(headers);
  
  http.StreamedResponse response = await request.send();
  
  if (response.statusCode == 200) {
    print(await response.stream.bytesToString());
  }
  else {
    print(response.reasonPhrase);
  }
  
}

if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
try {
let jsonBody = JSON.parse(body);
return `request.body = json.encode(${JSON.stringify(jsonBody, null, 4)});`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The space will depend on the indentation right? @webholik

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@umeshp7 The problem is that if the indentation is using tabs, then we have no way to handle that with JSON.stringify. So I decided to use constant indentation for all cases.

@webholik webholik merged commit 24d796d into develop May 6, 2021
@webholik webholik deleted the feature/json-formatting branch May 6, 2021 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C# use verbatim string

3 participants