Http post请求在postman中可以正常工作,但在iOS中不行?
我正在使用RestKit发出网络请求。我用的是basic auth。我正在给服务器打电话。这给了我一个错误。但我接受了相同的请求主体和标题,并在postman中尝试了相同的请求,它运行良好 swift编码Http post请求在postman中可以正常工作,但在iOS中不行?,ios,objective-c,swift,restkit,Ios,Objective C,Swift,Restkit,我正在使用RestKit发出网络请求。我用的是basic auth。我正在给服务器打电话。这给了我一个错误。但我接受了相同的请求主体和标题,并在postman中尝试了相同的请求,它运行良好 swift编码 let requestMapping: RKObjectMapping = self.defineSyncUserProfileRequestMapping().inverseMapping() objectManager.addRequestDescriptor(RKRe
let requestMapping: RKObjectMapping = self.defineSyncUserProfileRequestMapping().inverseMapping()
objectManager.addRequestDescriptor(RKRequestDescriptor(mapping: requestMapping, objectClass: UserProfile.self, rootKeyPath: nil, method: RKRequestMethod.POST))
let responseMapping = self.defineSyncUserProfileResponseMapping() as RKDynamicMapping
let responseDesriptor = RKResponseDescriptor(mapping: responseMapping, method: RKRequestMethod.POST, pathPattern: HabitsConstants.WebServicePath.POST_USER_PROFILE, keyPath: "", statusCodes: RKStatusCodeIndexSetForClass(UInt(RKStatusCodeClassSuccessful)))
objectManager.addResponseDescriptor(responseDesriptor)
objectManager.requestSerializationMIMEType = RKMIMETypeJSON
self.objectManager.HTTPClient .setAuthorizationHeaderWithUsername(self.userCredential.username, password: self.userCredential.password)
self.objectManager.HTTPClient.setDefaultHeader("Content-type" , value: RKMIMETypeJSON)
RKMIMETypeSerialization.registerClass(RKNSJSONSerialization.self, forMIMEType: "text/json")
objectManager.postObject(userProfile, path: URL, parameters: nil,
success:{ operation, mappingResult in
let response: NSArray = mappingResult.array()
if response.count > 0 {
if (response.objectAtIndex(0) .isKindOfClass(Error)) {
let error: Error = response.objectAtIndex(0) as! Error
NSLog("ERROR \(error.errorReason)");
self.delegate?.handleMessage(MessageType.ERROR, data: 1.0,errorMessage: error.errorReason)
} else if (response.objectAtIndex(0) .isKindOfClass(UserCredentialResponse)) {
NSLog("Saved profile for user\(userProfile.userId)");
self.getUserProfile()
self.getUserTargets()
}
}
},
failure:{ operation, error in
NSLog("Error: \(error!.localizedDescription)")
self.delegate?.handleMessage(MessageType.UNKOWN_ERROR, data: 1.0,errorMessage: "")
}
)
邮政编码
POST /dmanager/v5/updateprofile/ HTTP/1.1
Host: host.com
Content-Type: application/json
Authorization: Basic aHNoc25hbmE4MjMxOmpzanNqc2pzampz
Cache-Control: no-cache
Postman-Token: 23a32222-db02-b8bb-63cb-da5faffc27be
{"age":23,"phone_number":"","user_id":19709,"weight":25,"key":"jsjsjsjsjjs","set_first_login":"true","first_name":"Suisse","height":91.44,"last_name":"","email":"hshsnana@jsjsja.com","gender":"M"}
我也有类似的问题。来自Swift的请求主体与Postman请求完全相同,我从Django Rest框架API主机得到了错误的请求响应 Postman将
Content-Type
标题添加为值application/json
,如果这是您指定的主体类型。我试着在我的Swift iOS请求客户端(参见FMI)中自己设置它,现在我得到了成功的响应
我使用的相关代码片段:
var-request=URLRequest(url:renderedUrl)
request.setValue(“application/json;charset=utf-8”,forHTTPHeaderField:“内容类型”)
request.setValue(“application/json;charset=utf-8”,forHTTPHeaderField:“Accept”)
您可能需要在Swift中显示您的代码,并且可能需要在POSTMan中显示“那个”。我们不能在看不到代码的情况下猜出哪里出了问题。@Larme我在swift中添加了代码,postmanI建议使用Charles Proxy检查您发送的确切内容。从您的代码中,我看到您为text/json
注册了serialiser,但没有注册application/json
。你确定这是对的吗?