Skip to content

Commit 2d8d194

Browse files
committed
add CreateDiscussion.java: It's the first time add flarum to this project
1 parent 14730b8 commit 2d8d194

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package top.mryan2005.simplifiedjava.flarum;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStream;
5+
import java.io.InputStreamReader;
6+
import java.io.OutputStream;
7+
import java.net.HttpURLConnection;
8+
import java.net.URL;
9+
10+
public class CreateDiscussion {
11+
12+
private String title;
13+
private String content;
14+
private String[] tags;
15+
private String token;
16+
private String hostUrl;
17+
18+
public CreateDiscussion(String hostUrl, String token, String title, String content, String[] tags) {
19+
this.title = title;
20+
this.content = content;
21+
this.tags = tags;
22+
this.token = token;
23+
this.hostUrl = hostUrl;
24+
}
25+
26+
public StringBuffer submit() {
27+
try {
28+
// URL of the server endpoint
29+
URL url = new URL(hostUrl + "/api/discussions");
30+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
31+
32+
// Set the request method to POST
33+
connection.setRequestMethod("POST");
34+
connection.setDoOutput(true);
35+
36+
// Set request headers
37+
connection.setRequestProperty("Content-Type", "application/json");
38+
connection.setRequestProperty("Accept", "application/json");
39+
connection.setRequestProperty("Authorization", "Token " + this.token);
40+
41+
// JSON payload
42+
String jsonInputString = String.format(
43+
"{" +
44+
"\"data\": {" +
45+
"\"type\": \"discussions\"," +
46+
"\"attributes\": {" +
47+
"\"title\": \"%s\"," +
48+
"\"content\": \"%s\"" +
49+
"}," +
50+
"\"relationships\": {" +
51+
"\"tags\": {" +
52+
"\"data\": [" +
53+
"%s" +
54+
"]" +
55+
"}" +
56+
"}" +
57+
"}" +
58+
"}",
59+
title, content, String.join(",", tags)
60+
);
61+
62+
// Send the request
63+
try (OutputStream os = connection.getOutputStream()) {
64+
byte[] input = jsonInputString.getBytes("utf-8");
65+
os.write(input, 0, input.length);
66+
}
67+
68+
// Get the response code
69+
int responseCode = connection.getResponseCode();
70+
System.out.println("Response Code: " + responseCode);
71+
72+
// Handle the response (optional)
73+
InputStream inputStream = connection.getInputStream();
74+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
75+
String line;
76+
StringBuffer response = new StringBuffer();
77+
while ((line = reader.readLine()) != null) {
78+
response.append(line);
79+
}
80+
reader.close();
81+
return response;
82+
83+
} catch (Exception e) {
84+
e.printStackTrace();
85+
}
86+
return null;
87+
}
88+
89+
public static void main(String[] args) {
90+
String hostUrl = "https://xxx.com";
91+
String token = "xxx";
92+
String title = "Hello World";
93+
String content = "This is a test discussion";
94+
String[] tags = new String[]{"{\"type\": \"tags\", \"id\": \"19\"}"};
95+
CreateDiscussion createDiscussion = new CreateDiscussion(
96+
hostUrl, token, title, content, tags
97+
);
98+
createDiscussion.submit();
99+
}
100+
}

0 commit comments

Comments
 (0)