Skip to content
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

feat: add ability to set custom multipart boundary value #820

Merged
merged 2 commits into from
Aug 15, 2024

Conversation

PokeGuys
Copy link
Contributor

@PokeGuys PokeGuys commented Aug 13, 2024

Use case

Integrating with some web service that require a specific boundary format in multipart/form-data requests.

Some legacy service might requires a specific prefix in the boundary value, to ensure the compatibility, we need the ability to set a custom boundary value in the multipart request.

Example

package main

import (
	"crypto/rand"
	"fmt"
	"io"
	"log"

	"github.com/go-resty/resty/v2"
)

const BoundaryPrefix = "Some_MultiPart_Prefix_"

func randomBoundary() string {
	var buf [4]byte
	_, err := io.ReadFull(rand.Reader, buf[:])
	if err != nil {
		panic(err)
	}
	return fmt.Sprintf("%s_%X", BoundaryPrefix, buf[:])
}

func main() {
    // Initialize the client
    client := resty.New()

    // Perform the file upload
    _, err := client.R().
        SetMultipartFormData(map[string]string{"test": "1"}).
        SetBoundary(randomBoundary()) // Setting the custom boundary
        Post("http://example.com/upload")
    if err != nil {
        log.Fatalf("Failed to upload file: %v", err)
    }
}

@jeevatkm
Copy link
Member

@PokeGuys - Can you please add one practical use case and its example in the PR description?

@PokeGuys
Copy link
Contributor Author

@jeevatkm Hi. Updated.

request.go Outdated
@@ -458,6 +459,13 @@ func (r *Request) SetMultipartFields(fields ...*MultipartField) *Request {
return r
}

// SetBoundary method sets the multipart boundary for the request
// By default a random boundary will be generated in `mime/multipart`.
func (r *Request) SetBoundary(boundary string) *Request {
Copy link
Member

@jeevatkm jeevatkm Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PokeGuys Thanks for adding the use case detail to the PR description.

Can you refactor this method as below and it includes updated godoc and version info too?

// SetMultipartBoundary method sets the custom multipart boundary for the multipart request.
// Typically, the `mime/multipart` package generates a random multipart boundary, if not provided.
//
// Since v2.15.0
func (r *Request) SetMultipartBoundary(boundary string) *Request {
 	r.multipartBoundary = boundary
 	return r
 }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@jeevatkm jeevatkm added this to the v2.15.0 milestone Aug 15, 2024
* clarify `SetMultipartBoundary` method comment
* rename method name from `SetBoundary` to `SetMultipartBoundary`
@PokeGuys PokeGuys requested a review from jeevatkm August 15, 2024 15:19
Copy link
Member

@jeevatkm jeevatkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @PokeGuys, for the PR.

Copy link

codecov bot commented Aug 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.68%. Comparing base (231e19c) to head (9a5bfa7).
Report is 1 commits behind head on v2.

Additional details and impacted files
@@            Coverage Diff             @@
##               v2     #820      +/-   ##
==========================================
+ Coverage   96.67%   96.68%   +0.01%     
==========================================
  Files          14       14              
  Lines        1742     1748       +6     
==========================================
+ Hits         1684     1690       +6     
  Misses         37       37              
  Partials       21       21              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jeevatkm jeevatkm merged commit f575bf6 into go-resty:v2 Aug 15, 2024
3 checks passed
@PokeGuys PokeGuys deleted the feature/custom-boundary branch August 15, 2024 16:25
renovate bot added a commit to Michsior14/transmission-gluetun-port-update that referenced this pull request Sep 15, 2024
This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[github.com/go-resty/resty/v2](https://redirect.github.com/go-resty/resty)
| `v2.14.0` -> `v2.15.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fgo-resty%2fresty%2fv2/v2.15.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fgo-resty%2fresty%2fv2/v2.15.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fgo-resty%2fresty%2fv2/v2.14.0/v2.15.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fgo-resty%2fresty%2fv2/v2.14.0/v2.15.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>go-resty/resty (github.com/go-resty/resty/v2)</summary>

###
[`v2.15.0`](https://redirect.github.com/go-resty/resty/releases/tag/v2.15.0)

[Compare
Source](https://redirect.github.com/go-resty/resty/compare/v2.14.0...v2.15.0)

### Release Notes

#### New Features

- feat: add max reponse body limit by
[@&#8203;trim21](https://redirect.github.com/trim21) in
[go-resty/resty#830
- feat: add SetClientRootCertificate method support clientCAs usage by
[@&#8203;MagHErmit](https://redirect.github.com/MagHErmit) in
[go-resty/resty#826

#### Enhancements

- feat: add ability to set custom multipart boundary value by
[@&#8203;PokeGuys](https://redirect.github.com/PokeGuys) in
[go-resty/resty#820
- feat(refactor): for pr
[#&#8203;826](https://redirect.github.com/go-resty/resty/issues/826) by
[@&#8203;jeevatkm](https://redirect.github.com/jeevatkm) in
[go-resty/resty#848

#### Bug Fixes

- Fix request/response logging for SetDoNotParseResponse(true) by
[@&#8203;kon3gor](https://redirect.github.com/kon3gor) in
[go-resty/resty#836
- fix(enhancement): add explicit option to enable generate curl command
in conjunction with debug mode and few clean ups
[#&#8203;828](https://redirect.github.com/go-resty/resty/issues/828) by
[@&#8203;jeevatkm](https://redirect.github.com/jeevatkm) in
[go-resty/resty#842

#### Build

- feat(enhancement): update bazel config by
[@&#8203;frank30941](https://redirect.github.com/frank30941) in
[go-resty/resty#833
- build: go min version and build config update
[#&#8203;835](https://redirect.github.com/go-resty/resty/issues/835) by
[@&#8203;jeevatkm](https://redirect.github.com/jeevatkm) in
[go-resty/resty#837
- Update go vesion to 1.20 by
[@&#8203;kon3gor](https://redirect.github.com/kon3gor) in
[go-resty/resty#841

#### Documentation

- doc: godoc improvements and corrections by
[@&#8203;jeevatkm](https://redirect.github.com/jeevatkm) in
[go-resty/resty#849,
[go-resty/resty#851
- doc(readme): Add a note, GenerateCurlCommand by
[@&#8203;ahuigo](https://redirect.github.com/ahuigo) in
[go-resty/resty#817
- release: version bump and readme update for v2.15.0 by
[@&#8203;jeevatkm](https://redirect.github.com/jeevatkm) in
[go-resty/resty#852

#### New Contributors

- [@&#8203;PokeGuys](https://redirect.github.com/PokeGuys) made their
first contribution in
[go-resty/resty#820
- [@&#8203;trim21](https://redirect.github.com/trim21) made their first
contribution in
[go-resty/resty#830
- [@&#8203;frank30941](https://redirect.github.com/frank30941) made
their first contribution in
[go-resty/resty#833
- [@&#8203;kon3gor](https://redirect.github.com/kon3gor) made their
first contribution in
[go-resty/resty#841
- [@&#8203;MagHErmit](https://redirect.github.com/MagHErmit) made their
first contribution in
[go-resty/resty#826

**Full Changelog**:
go-resty/resty@v2.14.0...v2.15.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend Renovate](https://mend.io/renovate/).
View the [repository job
log](https://developer.mend.io/github/Michsior14/transmission-gluetun-port-update).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC43NC4xIiwidXBkYXRlZEluVmVyIjoiMzguNzQuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiZGVwZW5kZW5jaWVzIl19-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants