-
-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(easy): add post_string() wrapper with example
Add post_string() wrapper that completes the set of string response handlers, providing consistent string handling across request types
- Loading branch information
Showing
2 changed files
with
64 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,7 @@ main :: proc() { | |
} | ||
|
||
{ | ||
fmt.println("\n# String response:") | ||
fmt.println("\n# GET with string response:") | ||
res := curl.get_string("https://httpbin.org/json", config) | ||
defer curl.destroy_response(&res) | ||
|
||
|
@@ -69,7 +69,7 @@ main :: proc() { | |
} | ||
|
||
{ | ||
fmt.println("\n# JSON GET with parsing:") | ||
fmt.println("\n# GET with JSON response:") | ||
res := curl.get_json(Post, "https://jsonplaceholder.typicode.com/posts/1", config) | ||
defer curl.destroy_response(&res) | ||
|
||
|
@@ -82,7 +82,7 @@ main :: proc() { | |
} | ||
|
||
{ | ||
fmt.println("\n# Custom parser GET:") | ||
fmt.println("\n# GET with custom parser:") | ||
|
||
line_counter := curl.Parser(Line_Count){ | ||
parse = proc(data: []byte, allocator: runtime.Allocator, err: ^curl.Error) -> Line_Count { | ||
|
@@ -143,7 +143,53 @@ main :: proc() { | |
} | ||
|
||
{ | ||
fmt.println("\n# Custom parser POST:") | ||
fmt.println("\n# POST with string response:") | ||
body := "Hello Server!" | ||
res := curl.post_string("https://httpbin.org/post", body, config) | ||
defer curl.destroy_response(&res) | ||
|
||
if res.error != .None { | ||
fmt.eprintln("Request failed:", curl.error_string(res.error)) | ||
return | ||
} | ||
|
||
fmt.printf("Status: %d\nResponse: %s\n", res.status_code, res.body) | ||
} | ||
|
||
{ | ||
fmt.println("\n# POST with JSON response:") | ||
new_comment := Comment{ | ||
postId = 1, | ||
name = "Test Comment", | ||
email = "[email protected]", | ||
body = "This is a test comment", | ||
} | ||
|
||
data, marshal_err := json.marshal(new_comment, allocator=allocator) | ||
if marshal_err != nil { | ||
fmt.eprintln("JSON marshal failed:", marshal_err) | ||
return | ||
} | ||
defer delete(data) | ||
|
||
res := curl.post_json(Comment, | ||
"https://jsonplaceholder.typicode.com/comments", | ||
string(data), | ||
config) | ||
defer curl.destroy_response(&res) | ||
|
||
if res.error != .None { | ||
fmt.eprintln("Request failed:", curl.error_string(res.error)) | ||
return | ||
} | ||
|
||
fmt.printf("Created comment: ID %d for post %d\n", | ||
res.body.id, res.body.postId) | ||
fmt.printf("Comment text: %s\n", res.body.body) | ||
} | ||
|
||
{ | ||
fmt.println("\n# POST with custom parser:") | ||
|
||
line_counter := curl.Parser(Line_Count){ | ||
parse = proc(data: []byte, allocator: runtime.Allocator, err: ^curl.Error) -> Line_Count { | ||
|
@@ -204,36 +250,4 @@ main :: proc() { | |
fmt.printf("%d: %s\n", i+1, line) | ||
} | ||
} | ||
|
||
{ | ||
fmt.println("\n# JSON POST example:") | ||
new_comment := Comment{ | ||
postId = 1, | ||
name = "Test Comment", | ||
email = "[email protected]", | ||
body = "This is a test comment", | ||
} | ||
|
||
data, marshal_err := json.marshal(new_comment, allocator=allocator) | ||
if marshal_err != nil { | ||
fmt.eprintln("JSON marshal failed:", marshal_err) | ||
return | ||
} | ||
defer delete(data) | ||
|
||
res := curl.post_json(Comment, | ||
"https://jsonplaceholder.typicode.com/comments", | ||
string(data), | ||
config) | ||
defer curl.destroy_response(&res) | ||
|
||
if res.error != .None { | ||
fmt.eprintln("Request failed:", curl.error_string(res.error)) | ||
return | ||
} | ||
|
||
fmt.printf("Created comment: ID %d for post %d\n", | ||
res.body.id, res.body.postId) | ||
fmt.printf("Comment text: %s\n", res.body.body) | ||
} | ||
} |