如何使用Moya使用get-API(具有以下请求结构)?
enum MyApiTarget: TargetType {
case getInfo(contactID: Int, types: [String])
public var baseURL: URL {
switch self {
case .getInfo:
return URL(string: "http://some.environment.com/some/api")!
}
}
public var path: String {
switch self {
case .getInfo(contactID: let contactId, types: _):
return "/some/contacts/\(contactId)"
}
}
public var method: Moya.Method {
switch self {
case .getInfo:
return .get
}
}
public var sampleData: Data {
return Data()
}
public var task: Task {
switch self {
case .getInfo(contactID: _, types: let types):
return .requestParameters(
parameters: ["types" : types],
encoding: URLEncoding.queryString
)
}
}
public var headers: [String: String]? {
return nil
}
}
这是我尝试过的。
http://some.environment.com/some/api/some/contacts/81193?types%5B%5D=details&types%5B%5D=permissions
以上代码生成以下网址。
URLEncoding.queryString
我尝试过以下编码
URLEncoding.default
URLEncoding.httpBody
JSONEncoding.default
JSONEncoding.prettyPrinted
Private Sub BtnSendString1_Click(sender As Object, e As RoutedEventArgs)
If (sock.socket.Connected = False) Then
LblSock.Text += vbCrLf + "connection closed" + vbCrLf
Exit Sub
End If
Dim rand As Random = New Random()
Dim masking_key(4) As Byte
Dim i As Integer
masking_key(0) = Convert.ToByte(rand.Next(0, 250))
masking_key(1) = Convert.ToByte(rand.Next(0, 250))
masking_key(2) = Convert.ToByte(rand.Next(0, 250))
masking_key(3) = Convert.ToByte(rand.Next(0, 250))
Dim request As String = txtsock.Text
If (txtsock.Text.Length <= 125) Then
request = "112222" & request
Dim ascii As Encoding = Encoding.UTF8
Dim bytesSent As Byte() = ascii.GetBytes(request)
bytesSent(0) = &H80 Or 1
bytesSent(1) = txtsock.Text.Length Or &H80
bytesSent(2) = masking_key(0)
bytesSent(3) = masking_key(1)
bytesSent(4) = masking_key(2)
bytesSent(5) = masking_key(3)
Dim s As Integer = 0
For i = 6 To bytesSent.Length - 1
bytesSent(i) = bytesSent(i) Xor masking_key(s)
s += 1
If s = 4 Then s = 0
Next
LblSock.Text += vbCrLf + "Sending string: " + txtsock.Text
LblSock.Text += vbCrLf + "Sending bytes: "
For i = 0 To bytesSent.Length - 1
LblSock.Text += bytesSent(i).ToString() + ","
Next
sock.socket.Send(bytesSent)
ElseIf (txtsock.Text.Length <= 65535) Then
request = "11111111" & request
Dim ascii As Encoding = Encoding.UTF8
Dim bytesSent As Byte() = ascii.GetBytes(request)
bytesSent(0) = &H80 Or 1
bytesSent(1) = 126 Or &H80
bytesSent(2) = BitConverter.GetBytes(txtsock.Text.Length)(1)
bytesSent(3) = BitConverter.GetBytes(txtsock.Text.Length)(0)
bytesSent(4) = masking_key(0)
bytesSent(5) = masking_key(1)
bytesSent(6) = masking_key(2)
bytesSent(7) = masking_key(3)
Dim s As Integer = 0
For i = 8 To bytesSent.Length - 1
bytesSent(i) = bytesSent(i) Xor masking_key(s)
s += 1
If s = 4 Then s = 0
Next
LblSock.Text += vbCrLf + "Sending string: " + txtsock.Text
LblSock.Text += vbCrLf + "Sending bytes: "
For i = 0 To bytesSent.Length - 1
LblSock.Text += bytesSent(i).ToString() + ","
Next
sock.socket.Send(bytesSent)
Else
request = "1111111112222" & request
Dim ascii As Encoding = Encoding.UTF8
Dim bytesSent As Byte() = ascii.GetBytes(request)
bytesSent(0) = &H80 Or 1
bytesSent(1) = 126 Or &H80
bytesSent(2) = BitConverter.GetBytes(txtsock.Text.Length)(0)
bytesSent(3) = BitConverter.GetBytes(txtsock.Text.Length)(1)
bytesSent(4) = BitConverter.GetBytes(txtsock.Text.Length)(2)
bytesSent(5) = BitConverter.GetBytes(txtsock.Text.Length)(3)
bytesSent(6) = BitConverter.GetBytes(txtsock.Text.Length)(4)
bytesSent(7) = BitConverter.GetBytes(txtsock.Text.Length)(5)
bytesSent(8) = BitConverter.GetBytes(txtsock.Text.Length)(6)
bytesSent(9) = BitConverter.GetBytes(txtsock.Text.Length)(7)
bytesSent(10) = masking_key(0)
bytesSent(11) = masking_key(1)
bytesSent(12) = masking_key(2)
bytesSent(13) = masking_key(3)
Dim s As Integer = 0
For i = 13 To bytesSent.Length - 1
bytesSent(i) = bytesSent(i) Xor masking_key(s)
s += 1
If s = 4 Then s = 0
Next
LblSock.Text += vbCrLf + "Sending string: " + txtsock.Text
LblSock.Text += vbCrLf + "Sending bytes: "
For i = 0 To bytesSent.Length - 1
LblSock.Text += bytesSent(i).ToString() + ","
Next
sock.socket.Send(bytesSent)
End If
LblSock.ScrollToEnd()
End Sub
这些编码都没有帮助我产生预期的结果。
答案 0 :(得分:0)
我不知道MOYA
我有类似的问题和类似的结构,你已经用
解决了这个问题public protocol Router:URLRequestConvertible {
var endPoint : String {get}
var parameters:Parameters? {get}
var httpMethod : HTTPMethod {get}
var encoding:ParameterEncoding {get}
func asURLRequest() throws -> URLRequest
}
现在我有扩展程序,可以从包含encoding:ParameterEncoding
extension Router {
func asURLRequest() throws -> URLRequest {
// Generate URL
let url = try Constants.APIConstants.BaseURL.production.rawValue.asURL()
// Generate URL Request
var urlRequest = URLRequest(url: url.appendingPathComponent(endPoint))
// Generate Headers
urlRequest.addValue("application/json", forHTTPHeaderField:"Content-Type")
urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")
urlRequest.addValue(Constants.APIConstants.RequestKeys.APIKEY_Val, forHTTPHeaderField: Constants.APIConstants.RequestKeys.APIKEY)
urlRequest.addValue(Constants.APIConstants.RequestKeys.APIVERSION_Val , forHTTPHeaderField:Constants.APIConstants.RequestKeys.APIVERSION )
urlRequest.addValue(authToken, forHTTPHeaderField:Constants.APIConstants.RequestKeys.AUTHTOKEN )
urlRequest.httpMethod = self.httpMethod.rawValue
if let parameters = parameters {
do {
urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options:[])
urlRequest = try self.encoding.encode(urlRequest, with: parameters)
} catch {
throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
}
}
return urlRequest
}
}
现在我有枚举
enum APIRouterUserModule : Router {
case login(email:String,password:String)
// Implement all the Router protocol property (similar to your strct.) here is example for encoding
var encoding: ParameterEncoding {
switch self {
case .login :
return URLEncoding.queryString
//Return any of them
/*
URLEncoding.queryString
URLEncoding.default
URLEncoding.httpBody
JSONEncoding.default
JSONEncoding.prettyPrinted */
}
}
}
希望它有用
答案 1 :(得分:0)
我通过如下代码解决了这个问题。
按如下方式替换任务。
JSON
目前,我已完成手动JSON
编码。替代方法是首先将数据转换为JSON
&amp;来自{{1}}创建字符串&amp;供应