Skip to content

Commit

Permalink
feat(easy): add post_string() wrapper with example
Browse files Browse the repository at this point in the history
Add post_string() wrapper that completes the set of string
response handlers, providing consistent string handling across
request types
  • Loading branch information
zolk3ri committed Jan 12, 2025
1 parent 7b4196c commit 2e6ffa1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 36 deletions.
14 changes: 14 additions & 0 deletions vendor/curl/easy.odin
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,20 @@ post_with :: proc($T: typeid, url: string, data: string, parser: Parser(T), conf
return do_method_with(.Post, T, url, data, parser, config)
}

// post_string performs a POST request returning a string response.
//
// **Note**: The returned Response must be freed with destroy_response.
//
// Inputs:
// - url: Target URL to request
// - data: Data to send in request body
// - config: Optional request configuration including headers, timeouts, etc.
//
// Returns: Response containing string body.
post_string :: proc(url: string, data: string, config := Request_Config{}) -> Response(string) {
res := post_with(string, url, data, string_parser, config)
res.parser_type = .String
return res
}

// post_json performs a POST request with JSON response.
Expand Down
86 changes: 50 additions & 36 deletions vendor/curl/examples/typed.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 2e6ffa1

Please sign in to comment.