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

[Bug]: Body's specific headers overrides all the request/client set headers #62

Closed
dariopellegrini opened this issue Oct 7, 2022 · 5 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@dariopellegrini
Copy link

Platform Version

iOS 16

SDK Version

1.7.2

Xcode Version

Xcode 14

Steps To Reproduce

I have this request

try await HTTPRequest {
            $0.path = "businesscards"
            $0.headers = HTTPHeaders([
                .authorization:"Bearer \(token)",
                .contentType:"multipart/form-data"
            ])
            
            $0.method = .post
            $0.body = try .multipart(boundary: nil, { form in
                if let name = businessCardCreation.name {
                    try form.add(string: name, name: "name")
                }
                if let surname = businessCardCreation.surname {
                    try form.add(string: surname, name: "surname")
                }
                if let email = businessCardCreation.email {
                    try form.add(string: email, name: "email")
                }
            })
        }.fetch().decode(BusinessCard.self, decoder: Decoders.decoder)

that is not sending Authorization header to the backend.

Expected Behavior

I expect to see the Authorization header in the backend.

Actual Incorrect Behavior

I can't see the authorization header in the backend.
Here is a screenshot of the headers sent in a local call, but the same happens on the cloud.

Schermata 2022-10-07 alle 07 36 56

Trying the same request using Alamofire everything works.
Schermata 2022-10-07 alle 08 02 21

@dariopellegrini dariopellegrini added the bug Something isn't working label Oct 7, 2022
@nashfive
Copy link

nashfive commented Oct 7, 2022

Have you tried to use the HTTPHeader.authBearerToken("") instead ?

@malcommac
Copy link
Collaborator

malcommac commented Oct 9, 2022

Hi @dariopellegrini , thank you for your report.

Unfortunately, @nashfive, It's a bug: due to a design choice, the headers property is just a forward to body.headers property, so when you assign the body to the request, it will override any previously set headers property.
We'll provide a fix ASAP.

Meanwhile, a possible workaround could be to set the $0.headers= after the body, and it should work fine.

@malcommac malcommac self-assigned this Oct 9, 2022
@malcommac malcommac added this to the 1.7.3 milestone Oct 9, 2022
@malcommac malcommac changed the title [Bug]: Multipart-Form-Data requests are not sending Authorization header [Bug]: Body's specific headers overrides all the request/client set headers Oct 9, 2022
@malcommac
Copy link
Collaborator

It will be merged in #63 with a minor release marked as 1.7.3.
An additional unit test was also added to mark and validate any further changes.
Thank you very much for your report.

@malcommac
Copy link
Collaborator

malcommac commented Oct 9, 2022

1.7.3 was published few minutes ago, and it's available both on SPM and CocoaPods.

@dariopellegrini
Copy link
Author

@malcommac
I confirm that the bug is fixed in 1.7.3.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants