我想一個POST體發送到使用的iOS(SWIFT)由AWS API網關產生SDK一個AWS LAMBDA功能,但發送的身體總是空的。API網關:空請求正文?
爲此我成立了一個模型:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "InputModel",
"type": "object",
"required" : ["name"],
"properties": {
"name": { "type": "string" },
}
}
,我包括在$資源 - > /測試 - > POST - >方法執行 - >方法請求 - >請求體面板。
然後我部署了API並下載幷包含了API網關生成的SDK。現在我調用API(如Use Generated iOS SDK (Swift) to Call API推薦),像這樣:
let apiClient = APITestClient.default()
let body = APITest()
body!.name = "HOLA"
apiClient.testPost(body: body!).continueWith { task -> Void in ... }
但體我lambda函數接收總是空{}
,這是我在API網關日誌檢查:
(c421a895-b0c7-11e7-89a6-891dcbadf1f0) Method request body before transformations:
{}
和lambda函數日誌:
2017-10-14T10:09:29.418Z c424b611-b0c7-11e7-99c1-251bdb6a1eac Request:
{
"resource": "/test",
"path": "/test",
"httpMethod": "POST",
"headers": {
"Accept": "application/json",
"Accept-Encoding": "br, gzip, deflate",
"Accept-Language": "en-us",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "ES",
"content-type": "application/json",
"Host": "9hlt8n48wd.execute-api.eu-west-1.amazonaws.com",
"User-Agent": "aws-sdk-iOS/2.6.4 iOS/11.0 en_US",
"Via": "2.0 9ee2752d23ce7be13204dc3880c3ca6b.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "39uT89_umng7zPCNHqn3WdLlNJ6zFbQ5fmLVzJJNFcud-7pdZYJ1mg==",
"X-AMZ-Date": "20171014T100929Z",
"x-amz-security-token": "---",
"X-Amzn-Trace-Id": "---",
"X-Forwarded-For": "---",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"queryStringParameters": null,
"pathParameters": null,
"stageVariables": null,
"requestContext": {
"path": "/production/test",
"accountId": "037641059503",
"resourceId": "hfza9c",
"stage": "production",
"requestId": "---",
"identity": {
---
},
"resourcePath": "/test",
"httpMethod": "POST",
"apiId": "---"
},
"body": "{}",
"isBase64Encoded": false
}
爲什麼身體總是空的?我是否需要初始化與我不同的身體?還是有另一個階段,身體被清除?
編輯: 我還在掙扎,但是調用正常AWS iOS版SDK:
let apiClient = APITestClient.default()
let apiGatewayRequest = AWSAPIGatewayRequest(httpMethod: "POST", urlString: "/test", queryParameters: nil, headerParameters: nil, httpBody: ["name":"example"])
apiClient.invoke(apiGatewayRequest).continueWith { task -> Void in ... }
工作完全正常。爲什麼AWS甚至不打算生成一個特殊的API網關SDK?
編輯2:API模型
public class APITest : AWSModel {
var example: String?
public override static func jsonKeyPathsByPropertyKey() -> [AnyHashable : Any]!{
var params:[AnyHashable : Any] = [:]
params["name"] = "name"
return params
}
}
然後由
public func testPost() -> AWSTask<APITest> {
let headerParameters = [
"Content-Type": "application/json",
"Accept": "application/json",
]
let queryParameters:[String:Any] = [:]
let pathParameters:[String:Any] = [:]
return self.invokeHTTPRequest("POST", urlString: "/test", pathParameters: pathParameters, queryParameters: queryParameters, headerParameters: headerParameters, body: nil, responseClass: APITest.self) as! AWSTask<APITest>
}
你可以嘗試從API網關集成請求的響應模擬? –
我試過了,但是身體還是空的。是否生成的SDK與XCode 9.0中的Swift 4不兼容? – Knowledge
您可以使用Postman或其他HTTP客戶端測試您的API端點嗎? – dashmug