Http Snippet

An HTTP Request snippet generator for Springfox

License

License

Apache License 2.0
GroupId

GroupId

io.github.atkawa7
ArtifactId

ArtifactId

httpsnippet
Last Version

Last Version

1.0.7
Release Date

Release Date

Type

Type

jar
Description

Description

Http Snippet
An HTTP Request snippet generator for Springfox

Download httpsnippet

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.atkawa7/httpsnippet/ -->
<dependency>
    <groupId>io.github.atkawa7</groupId>
    <artifactId>httpsnippet</artifactId>
    <version>1.0.7</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.atkawa7/httpsnippet/
implementation 'io.github.atkawa7:httpsnippet:1.0.7'
// https://jarcasting.com/artifacts/io.github.atkawa7/httpsnippet/
implementation ("io.github.atkawa7:httpsnippet:1.0.7")
'io.github.atkawa7:httpsnippet:jar:1.0.7'
<dependency org="io.github.atkawa7" name="httpsnippet" rev="1.0.7">
  <artifact name="httpsnippet" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.atkawa7', module='httpsnippet', version='1.0.7')
)
libraryDependencies += "io.github.atkawa7" % "httpsnippet" % "1.0.7"
[io.github.atkawa7/httpsnippet "1.0.7"]

Dependencies

compile (3)

Group / Artifact Type Version
io.github.atkawa7 : har jar 1.1.4
org.apache.commons : commons-lang3 jar 3.9
org.apache.commons : commons-text jar 1.6

provided (3)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.11.1
com.github.spotbugs : spotbugs-annotations jar 3.1.12
org.projectlombok : lombok jar 1.18.12

test (8)

Group / Artifact Type Version
commons-io : commons-io jar 2.6
org.junit.jupiter : junit-jupiter-api jar 5.4.2
org.junit.jupiter : junit-jupiter-engine jar 5.4.2
org.junit.jupiter : junit-jupiter-params jar 5.4.2
org.junit.platform : junit-platform-commons jar 1.4.2
org.junit.platform : junit-platform-launcher jar 1.4.2
org.slf4j : slf4j-api jar 1.7.26
org.slf4j : slf4j-simple jar 1.7.26

Project Modules

There are no modules declared in this project.

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

Versions

Version
1.0.7