Code Monkey home page Code Monkey logo

httpsnippet's Introduction

HTTP Snippet

License Coveralls github Codecov GitHub issues Travis (.org)

HTTP Snippet port for java. See the original node port. Supports many languages & tools including: cURL, HTTPie, Javascript, Node, C, Java, PHP, Objective-C, Swift, Python, Ruby, C#, Go, OCaml and more!

The motivation behind porting this is using it for generating snippets in swagger and redocs. The project is still in development phase.

  • Documentation
  • Tests
  • Releasing to maven

Usage

Enable maven snapshots in ~/.m2/settings.xml

<profiles>
  <profile>
     <id>allow-snapshots</id>
        <activation><activeByDefault>true</activeByDefault></activation>
     <repositories>
       <repository>
         <id>snapshots-repo</id>
         <url>https://oss.sonatype.org/content/repositories/snapshots</url>
         <releases><enabled>false</enabled></releases>
         <snapshots><enabled>true</enabled></snapshots>
       </repository>
     </repositories>
   </profile>
</profiles>

Then add this dependency to pom.xml

<dependency>
     <groupId>io.github.atkawa7</groupId>
     <artifactId>httpsnippet</artifactId>
     <version>0.0.1-SNAPSHOT</version>
</dependency>

Here is the code for generating snippet

public class Main {
     public static void main(String[] args) throws Exception {
         List<HarHeader> headers = new ArrayList<>();
         List<HarQueryString> queryStrings = new ArrayList<>();
 
         User user = new User();
         Faker faker = new Faker();
         user.setFirstName(faker.name().firstName());
         user.setLastName(faker.name().lastName());
 
 
         HarPostData harPostData =
                 new HarPostDataBuilder()
                         .withMimeType(MediaType.APPLICATION_JSON)
                         .withText(ObjectUtils.writeValueAsString(user)).build();
 
         HarRequest harRequest =
                 new HarRequestBuilder()
                         .withMethod(HttpMethod.GET.toString())
                         .withUrl("http://localhost:5000/users")
                         .withHeaders(headers)
                         .withQueryString(queryStrings)
                         .withHttpVersion(HttpVersion.HTTP_1_1.toString())
                         .withPostData(harPostData)
                         .build();
 
         //Using default client
         HttpSnippet httpSnippet = new HttpSnippetCodeGenerator().snippet(harRequest, Language.JAVA);
         System.out.println(httpSnippet.getCode());
 
         //Or directly using
         String code   = new OkHttp().code(harRequest);
         System.out.println(code);
 
     }
 
     @Data
     static class User {
         private String firstName;
         private String lastName;
     }
 }

The result

HttpResponse<String> response = Unirest.get("http://localhost:5000/users")
  .body("{\"firstName\":\"Burton\",\"lastName\":\"Greenholt\"}")
  .asString();

Running the demo application

mvn clean install
java -jar httpsnippet-demo/target/httpsnippet-demo-0.0.1-SNAPSHOT.jar

After running the demo here are the results

Alt text

Alt text

License

Apache 2.0 © atkawa7

httpsnippet's People

Contributors

atkawa7 avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

httpsnippet's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.