Skip to content

Repository files navigation

HTTP Snippet

LicenseCoveralls githubCodecovGitHub issuesTravis (.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

publicclassMain {
publicstaticvoidmain(String[] args) throwsException {
List<HarHeader> headers = newArrayList<>();
List<HarQueryString> queryStrings = newArrayList<>();
Useruser = newUser();
Fakerfaker = newFaker();
user.setFirstName(faker.name().firstName());
user.setLastName(faker.name().lastName());
HarPostDataharPostData =
newHarPostDataBuilder()
.withMimeType(MediaType.APPLICATION_JSON)
.withText(ObjectUtils.writeValueAsString(user)).build();
HarRequestharRequest =
newHarRequestBuilder()
.withMethod(HttpMethod.GET.toString())
.withUrl("http://localhost:5000/users")
.withHeaders(headers)
.withQueryString(queryStrings)
.withHttpVersion(HttpVersion.HTTP_1_1.toString())
.withPostData(harPostData)
.build();
//Using default clientHttpSnippethttpSnippet = newHttpSnippetCodeGenerator().snippet(harRequest, Language.JAVA);
System.out.println(httpSnippet.getCode());
//Or directly usingStringcode = newOkHttp().code(harRequest);
System.out.println(code);
}
@DatastaticclassUser {
privateStringfirstName;
privateStringlastName;
}
}

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

About

An HTTP Request snippet generator for Springfox

Topics

Resources

Stars

20 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages