-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Post request with parameters in both body and url #909
Comments
You can change the used ParameterEncoding. An idea could be to create a ParameterEncoding like: public struct TokenEncoding: ParameterEncoding {
public let token: String
private let urlEncoding = URLEncoding(destination: .methodDependent)
init(token: String) {
self.token = token
}
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
// use URLEncoding to encoding parameters
// use something like URLEncoding's implementation to add token to URL.
// URLEncoding can be found here: https://github.com/Alamofire/Alamofire/blob/master/Source/ParameterEncoding.swift#L70
}
} This does require that token stays the same I guess. Another way could have it pull out token from passed parameters and then treat the token parameter in a special way. Mind you all of this code has not been tested in any way. |
As @bjarkehs suggests, writing a custom Another option would be to create a |
I'll close this issue since it's been inactive for a while. Feel free to reopen if you still have questions, @kristiyandobrev |
@pedrovereza I just came to a similar issue again, so I consider re-openning the issue again, since I am uncertain if it's possible to make for example |
@kristiyandobrev I think what you're looking for is what @scottrhoyt suggested:
Basically, if you have an endpoint like: enum API {
case example(id:String, name:String, lastName: String)
} In order to make a request to
public var path: String {
switch self {
case .example(let id, _, _):
return "api/resource/\(id)"
}
}
public var parameters: [String : Any]? {
switch self {
case .example(let _, let name, let lastName):
return ["name": name, "lastName": lastName]
}
}
public var parameterEncoding: Moya.ParameterEncoding {
switch self {
case .example:
return JSONEncoding.default
}
} |
@kristiyandobrev were you able to make the request as you wanted? |
Yes. Thanks for the help . |
I want to call this https://www.blaalb.com/restApi/testApi/myOrders?number=1&month=24
|
Can you please help with the comment by @AhmAbdallah , it will help many others as well. |
@zohairhadi you should be able to do that easier now, with the |
I am not entirely sure if that is an issue but please feel free to prove me wrong.
What I am trying to achieve is to make a
POST
request to an API. The first thing is that I have to provide theTOKEN
for theAPI
, encodend in the url like:http://awesomeapi.com?token=mytoken
Unfortunately the
API
I am calling, is made in a way so that it accepts the token only if it's a URL encoded parameter.Next thing is I have to send a parameters in a
JSON
body like :So in general I have to make a
POST
request and provide both URL encoded parameters and body parameters.While researching about an answer I came into Issue 431, where the author is trying to achieve the same, but I can not see a straightforward answer to his question.
Reading the docs make me think that having parameters in the request body and as URL encoded is of course possible in separate requests, but having them combined in one is not or at least not documented yet. Am I right about that ? If not, can you provide an example of how can this be done ?
The text was updated successfully, but these errors were encountered: