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

Modify the body of a request after initialization before executing it #26

Closed
nashfive opened this issue Mar 17, 2022 · 2 comments
Closed
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@nashfive
Copy link

Hi! That's an awesome library :)
I am trying to find a replacement to my verbose Moya/Alamofire layer and I came across RealHTTP that works pretty well!
I am just trying to see what would be the best approach to intercept a request and mutate the request body. For example, I'd like to send URL encoded forms, and have an interceptor that would add some extra data on top on the initial ones.

Since req.body is a Data, the req.urlRequestModifier is not very helpful in this case :(
Any idea ?
Or should I resort to defer the creation of the HttpRequest at the very last step and pass a Dictionary around ?

@malcommac
Copy link
Collaborator

malcommac commented Mar 19, 2022

Hi @nashfive , thank you!
I've considered an interceptor to change only the body but it seems a bit constrained and probably someone, in the future, may also need to change other parameters before the execution of the request.
You can, however, change the body as you wish, just create and use it or cast the body of the request to your desired type or create the

Example 1: Create the type

Consider the multipart form; you can create form from any point of your code. Just use:

// create your own body object
let form = HTTPBody.MultipartForm()

// somewhere add p1
form.add(string: "value", name: "p1")

Before executing the call just set the body:

req.body = form

Example 2: Use Casting

Another example is to cast the body to your desidered type.
To simplify this behaviour I've added (commit above) the following shortcuts (you will find it in 1.3.0+):

  • asData and asString to cast body to Data and String
  • asFormURLEncoded to cast body to WWWFormURLEncodedBody
  • asMultipartForm to cast body to MultipartForm
  • asStream to cast body to asStream

and use the object to modify the content itself.
Consider the following example with form

let req = HTTPRequest {
  $0.url = URL(string: "https://somedomain.com")
  $0.method = .post
  $0.body = .multipart {
    // configure multipart
  }
}

then use asMultipartForm to cast and add your own additional params:

req.body.asMultipartForm?.add(string: "value 2", name: "p2")

For your specific issue consider the following init:

let req = HTTPRequest {
   $0.body = .formURLEncodedBody(["username": "Michael Bublé", "pwd": "abc"])
}

You can add or remove values:

req.body.asFormURLEncoded?.set(value: "newValue", forKey: "newKey")
req.body.asFormURLEncoded?.removeValueForKey("newKey")

@malcommac malcommac added the enhancement New feature or request label Mar 19, 2022
@malcommac malcommac self-assigned this Mar 19, 2022
@malcommac malcommac added this to the 1.3.0 milestone Mar 19, 2022
@malcommac malcommac changed the title Any way to have request interceptors to update the request body? Modify the body of a request after initialization before executing it Mar 19, 2022
malcommac added a commit that referenced this issue Mar 19, 2022
…serialize

#26 - Added shortcuts to cast body to common structures
@nashfive
Copy link
Author

Thanks for your feedback @malcommac !
I guess my use case is too specific, as usually interceptors usually updates the headers to add a token, or mutate the query parameters.
Anyway, I'll have a look at these new shortcuts in your 1.3.0 release ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants