
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
json2http is a CLI tool that depends on json2class for generating code from JSON or JSON5, enabling HTTP-based requests.
English | 简体中文
json2http is a command-line tool that relies on json2class to convert specified JSON(5) files into HTTP request code.
| dart@3 | arkTs@12 | typescript@5 | kotlin@1.3 | swift@5.7 |
| java | Other languages to be supported |
✅ Recommended Node, npm, and npx Development Environmentnpx requires a Node environment. Please install Node first.
npx json2http build -l dart@3
dart pub add dev:json2http
dart run json2http build -l dart@3
Due to the release restrictions of
OpenHarmony Third-party Library Center, from versionv0.0.14, independent executable files are no longer provided. Instead, JS scripts are provided and executed bynode. Fortunately,OpenHarmony Developer ToolsandDevEco-Studiocome withnode. Just add it to thePATHenvironment variable.
OpenHarmony Developer Tools'snodeis usually at: command-line-tools/tool/node/bin/nodeDevEco-Studio'snodeis usually at: DevEco-Studio.app/Contents/tools/node/bin/nodeThe above methods are a bit cumbersome, so it is still recommended to use the npx method for simplicity.
Add the following configuration to oh-package.json5.
{
"scripts": {
"json2http": "node ./oh_modules/json2http/src/main/resources/rawfile/json2http build -l arkTs@12",
}
}
Run the following commands to install.
ohpm install json2http --save-dev
ohpm run json2http
JSON files support both json and json5 formats.
// ~/projects/config/root.json
{
'/api/blood/index': {
title: 'Baby blood type calculation',
method: 'GET',
params: {
father: '',
mother: '',
},
res: {
code: 0,
msg: '',
data: {
possible: [''],
impossible: [''],
},
copyright: '',
},
},
}
By default, it searches for and converts json configurations in the current directory where the command is executed.
cd ~/projects/config/
npx json2http build -l dart@3
Usage of Code
import 'json2http.dart';
main() async {
// Global configuration
Json2http.setPlan = (p) {
p.baseURL = 'https://qqlykm.cn';
p.process = (reply) {
final data = reply.data;
if (data is Map) {
if (data['code'] != 200) {
// Set error to determine whether to throw an exception
// Avoid checking status code for every interface
reply.error = data['msg'];
}
}
};
};
// Initiate request
final plan = await Json2http.single.apibloodindex((p) {
// Parameter configuration for a single request
p.params.father = 'A';
p.params.mother = 'O';
});
// Return value
print(plan.res.toJson());
print(plan.res.msg);
print(plan.res.data.possible.elementAtOrNull(0));
print(plan.res.data.impossible.elementAtOrNull(0));
}
{
'/api/test/path': {
title: 'Short description of the interface',
method: 'POST',
headers: { 'x-some-key': '' },
params: {
args1: '', args2: '', args3: '...'
},
body: {
type: 'json',
data: {
args4: 0, args5: '', args6: true, args7: [''],
},
},
res: {
code: 0,
msg: '',
data: {
possible: [''],
impossible: [''],
},
copyright: '',
},
},
}
{
'/api/test/path': {
title: '',
method: 'GET',
params: { xxx: '', yyy: 123, zzz: true },
},
}
{
'/api/test/path1': {
title: '',
method: 'POST',
body: {
type: 'json',
data: 1,
},
},
'/api/test/path2': {
title: '',
method: 'POST',
body: {
type: 'json',
data: {
xxx: 0, yyy: [''], zzz: true
},
},
},
}
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'map',
data: { xxx: '', yyy: 123, zzz: true },
},
},
}
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'form',
data: {
fields: { xxx: '', yyy: 123 },
files: { aaa: [true], bbb: '' },
},
},
},
}
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'plain',
data: '',
},
},
}
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'byte',
data: '',
},
},
}
{
'/api/test/path': {
title: '',
method: 'GET',
res: {
statusCode: '',
statusMessage: '',
data: { xxx: '', yyy: 1 },
},
},
}
{
'/api/test/{user}': {
title: '',
method: 'GET',
},
}
main() async {
await Json2http.single.apitestuser((p) {
p.seg.user = 'user';
});
}
If you really don't like using the interface address as the method name, you can also customize the method name in this way.
{
apiMethodName: {
path: '/api/test/path',
title: '',
method: 'GET',
params: { xxx: '' },
},
}
main() async {
await Json2http.single.apiMethodName((p) {
p.params.xxx = 'user';
});
}
The code generated for the above fields will inherit json2class, and the configuration and usage of the corresponding values follow json2class, such as:
Similar to json2class, the configuration of json2http can also reference an existing structure through { $meta: { ref: '' } }.
The difference is that when referencing a parent structure different from the current one, json2class uses the file as the basic unit, while json2http uses the interface as the basic unit.
In the following example, res in interface /api/test/path2 can reuse res in /api/test/path1.
{
'/api/test/path1': {
title: '',
method: 'GET',
res: {
xxx: '',
},
},
'/api/test/path2': {
title: '',
method: 'GET',
res: {
$meta: { ref: '/api/test/path1#/res' },
},
},
}
If the reference occurs in the current interface, the current interface address can be omitted.
{
'/api/test/path': {
title: '',
method: 'GET',
params: { xxx: '' },
res: {
$meta: { ref: '#/params' },
},
},
}
By referencing its own parent, recursive types can be generated.
{
'/api/test/path': {
title: '',
method: 'GET',
res: {
someKey: '',
child: {
: { ref: '#/res' },
},
},
},
}
In these three cases, you can set ? mark for data, and the generated code can set data to null.
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'json',
'data?': 1,
},
},
}
main() async {
await Json2http.single.apitestpath((p) {
p.body.data = null;
});
}
import 'json2http.dart';
// You can customize the Agent to fully control the request call behavior
class XAgent extends Agent {
Future<Reply> fetch(Plan plan) async {
// Can be implemented based on the generated code
plan.reply.data = {'statusCode': '0'};
plan.reply.code = 200;
return plan.reply;
}
body(Plan plan) {
// Can be implemented based on the generated code
return null;
}
}
main() {
Json2http.setPlan = (plan) {
// Global configuration
plan.baseURL = 'http://localhost:3000';
plan.process = (reply) {
final data = reply.data;
if (data is Map) {
if (data['statusCode'] != '0') {
// Determine whether the final request throws an exception
reply.error = data['statusMessage'];
}
}
};
plan.ready = () {
// Set the original configuration in the original request tool
plan.option = xxx;
// Set the original request tool
plan.session = xxx;
};
if (plan is testagentplan) {
plan.agent = XAgent();
}
if (plan.title.startsWith('amap:')) {
// Special interface special configuration
plan.baseURL = 'https://restapi.amap.com/v3';
plan.path = '${plan.path}?key=xxx';
plan.process = (reply) {
var data = reply.data;
if (data is Map) {
if (data['status'] != '1') {
reply.error = data['info'];
}
}
};
}
if (plan is refreshtestplan) {
plan.after = () async {
if (plan.reply.error == 'small token is error') {
// Implement token refresh
final x = await Json2http.single.refreshtoken((p) {
p.headers = {'access-token': 'bearer xxx'};
});
// After re-setting the token, retry the interface
plan.headers['access-token'] = x.res.data;
await plan.fetch();
}
};
}
};
}
{
'/api/blood/index': {
title: 'Baby blood type calculation',
method: 'GET',
params: {
father: '',
mother: '',
},
res: {
code: 0,
msg: '',
data: {
possible: [''],
impossible: [''],
},
copyright: '',
},
},
}
main() async {
final result = await Json2http.single.apibloodindex((plan) {
plan.params.father = 'A';
plan.params.mother = 'B';
});
}
npx json2http build -l dart@3
npx json2http build -l dart@3 -s ~/projects/test/
By default, class files are generated in the directory where the JSON configurations are located.
cd ~/projects/test/
npx json2http build -l dart@3 -o ../cache/
Specifying the -o parameter allows you to define an output directory. It is recommended to add this directory or the generated files to .gitignore.
# .gitignore
~/projects/cache/
json2http.*
Thank you for using this tool! we would love to hear your feedback and suggestions. If you encounter any issues or have suggestions for improvement, please feel free to provide feedback through the following channels:
Your feedback is extremely important to us. Thank you very much!
FAQs
json2http is a CLI tool that depends on json2class for generating code from JSON or JSON5, enabling HTTP-based requests.
We found that json2http demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.