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
- Host: GitHub
- URL: https://github.com/atkawa7/httpsnippet
- Owner: atkawa7
- License: apache-2.0
- Created: 2019-03-03T05:17:59.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-12T07:27:09.000Z (9 months ago)
- Last Synced: 2025-04-24T06:06:24.583Z (6 months ago)
- Topics: har, redoc, snippet-generator, springfox, swagger
- Language: Java
- Homepage: https://atkawa7.github.io/httpsnippet
- Size: 2.65 MB
- Stars: 20
- Watchers: 2
- Forks: 5
- Open Issues: 35
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HTTP Snippet
[](https://opensource.org/licenses/Apache-2.0)




> 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


## License
[Apache 2.0](LICENSE) © [atkawa7](https://github.com/atkawa7/httpsnippet)
[license-url]: https://github.com/atkawa7/httpsnippet/blob/master/LICENSE