An open API service indexing awesome lists of open source software.

https://github.com/atkawa7/httpsnippet

An HTTP Request snippet generator for Springfox
https://github.com/atkawa7/httpsnippet

har redoc snippet-generator springfox swagger

Last synced: 6 months ago
JSON representation

An HTTP Request snippet generator for Springfox

Awesome Lists containing this project

README

          

# HTTP Snippet

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
![Coveralls github](https://img.shields.io/coveralls/github/atkawa7/httpsnippet.svg?label=jacoco)
![Codecov](https://img.shields.io/codecov/c/github/atkawa7/httpsnippet.svg?label=cobertura)
![GitHub issues](https://img.shields.io/github/issues/atkawa7/httpsnippet.svg)
![Travis (.org)](https://img.shields.io/travis/atkawa7/httpsnippet.svg?label=travis)
> HTTP Snippet port for java. See [the original node port](https://github.com/Kong/httpsnippet). 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***.

- [x] Documentation
- [x] Tests
- [ ] Releasing to maven

## Usage
Enable maven snapshots in `~/.m2/settings.xml`
```xml


allow-snapshots
true


snapshots-repo
https://oss.sonatype.org/content/repositories/snapshots
false
true


```
Then add this `dependency` to `pom.xml`

```xml

io.github.atkawa7
httpsnippet
0.0.1-SNAPSHOT

```

Here is the code for generating snippet

```java
public class Main {
public static void main(String[] args) throws Exception {
List headers = new ArrayList<>();
List 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

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

## Running the demo application

```sh
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](images/Redoc.png?raw=true "Redoc")

![Alt text](images/Swagger-UI.png?raw=true "Swagger UI")

## License

[Apache 2.0](LICENSE) © [atkawa7](https://github.com/atkawa7/httpsnippet)

[license-url]: https://github.com/atkawa7/httpsnippet/blob/master/LICENSE