Skip to content

cannot find the PUT and GET method implementation #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"github.com/andelf/go-curl"
)

// writeCallback is used to capture response data
func writeCallback(data []byte, userdata interface{}) bool {
fmt.Print(string(data))
return true
}

func main() {
// Initialize curl
easy := curl.EasyInit()
if easy == nil {
fmt.Println("Failed to initialize curl")
return
}
defer easy.Cleanup()

// Configure GET request
easy.Setopt(curl.OPT_URL, "https://jsonplaceholder.typicode.com/posts/1")
easy.Setopt(curl.OPT_HTTPGET, true) // Explicitly set GET method
easy.Setopt(curl.OPT_WRITEFUNCTION, writeCallback)

// Perform the request
if err := easy.Perform(); err != nil {
fmt.Printf("GET request failed: %v\n", err)
return
}
fmt.Println("\nGET request completed")
}
39 changes: 39 additions & 0 deletions examples/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"github.com/andelf/go-curl"
"strings"
"testing"
)

func TestGetRequest(t *testing.T) {
// Capture response
var response strings.Builder
writeCallback := func(data []byte, userdata interface{}) bool {
response.Write(data)
return true
}

// Initialize curl
easy := curl.EasyInit()
if easy == nil {
t.Fatal("Failed to initialize curl")
}
defer easy.Cleanup()

// Configure GET request
easy.Setopt(curl.OPT_URL, "https://jsonplaceholder.typicode.com/posts/1")
easy.Setopt(curl.OPT_HTTPGET, true)
easy.Setopt(curl.OPT_WRITEFUNCTION, writeCallback)

// Perform the request
if err := easy.Perform(); err != nil {
t.Fatalf("GET request failed: %v", err)
}

// Verify response contains expected JSON fields
resp := response.String()
if !strings.Contains(resp, `"id": 1`) {
t.Errorf("Expected response to contain id: 1, got: %s", resp)
}
}
41 changes: 41 additions & 0 deletions examples/put.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"github.com/andelf/go-curl"
"strings"
)

// writeCallback captures response data
func writeCallback(data []byte, userdata interface{}) bool {
fmt.Print(string(data))
return true
}

func main() {
// Initialize curl
easy := curl.EasyInit()
if easy == nil {
fmt.Println("Failed to initialize curl")
return
}
defer easy.Cleanup()

// JSON payload for the PUT request
payload := `{"id": 1, "title": "Updated Post", "body": "This is an updated post", "userId": 1}`
reader := strings.NewReader(payload)

// Configure PUT request
easy.Setopt(curl.OPT_URL, "https://jsonplaceholder.typicode.com/posts/1")
easy.Setopt(curl.OPT_CUSTOMREQUEST, "PUT") // Set method to PUT
easy.Setopt(curl.OPT_POSTFIELDS, payload) // Set request body
easy.Setopt(curl.OPT_HTTPHEADER, []string{"Content-Type: application/json"})
easy.Setopt(curl.OPT_WRITEFUNCTION, writeCallback)

// Perform the request
if err := easy.Perform(); err != nil {
fmt.Printf("PUT request failed: %v\n", err)
return
}
fmt.Println("\nPUT request completed")
}
44 changes: 44 additions & 0 deletions examples/put_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"github.com/andelf/go-curl"
"strings"
"testing"
)

func TestPutRequest(t *testing.T) {
// Capture response
var response strings.Builder
writeCallback := func(data []byte, userdata interface{}) bool {
response.Write(data)
return true
}

// Initialize curl
easy := curl.EasyInit()
if easy == nil {
t.Fatal("Failed to initialize curl")
}
defer easy.Cleanup()

// JSON payload
payload := `{"id": 1, "title": "Updated Post", "body": "Updated content", "userId": 1}`

// Configure PUT request
easy.Setopt(curl.OPT_URL, "https://jsonplaceholder.typicode.com/posts/1")
easy.Setopt(curl.OPT_CUSTOMREQUEST, "PUT")
easy.Setopt(curl.OPT_POSTFIELDS, payload)
easy.Setopt(curl.OPT_HTTPHEADER, []string{"Content-Type: application/json"})
easy.Setopt(curl.OPT_WRITEFUNCTION, writeCallback)

// Perform the request
if err := easy.Perform(); err != nil {
t.Fatalf("PUT request failed: %v", err)
}

// Verify response contains updated data
resp := response.String()
if !strings.Contains(resp, `"title": "Updated Post"`) {
t.Errorf("Expected response to contain updated title, got: %s", resp)
}
}