-
Notifications
You must be signed in to change notification settings - Fork 29
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
Comments
Hi @nashfive , thank you! Example 1: Create the typeConsider 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 CastingAnother example is to cast the
and use the object to modify the content itself. let req = HTTPRequest {
$0.url = URL(string: "https://somedomain.com")
$0.method = .post
$0.body = .multipart {
// configure multipart
}
} then use 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") |
…serialize #26 - Added shortcuts to cast body to common structures
Thanks for your feedback @malcommac ! |
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 aData
, thereq.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 ?
The text was updated successfully, but these errors were encountered: