Http post请求在postman中工作正常,但在iOS中没有?

时间:2016-09-16 13:59:56

标签: ios objective-c swift restkit

我正在使用RestKit发出网络请求。我正在使用基本身份验证。我正在尝试向服务器发帖。它给了我401错误。但我采取相同的请求身体和标题在邮递员尝试相同的要求,它的工作正常。

swift中的代码

Ü

发布手动代码

  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: "")
            }
        )

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。来自 Swift 的请求正文与 Postman 请求相同,我收到了来自 Django Rest Framework API 主机的错误请求响应。

Postman 添加值为 Content-Typeapplication/json 标头,如果这是您指定的正文类型。我尝试在我的 Swift iOS 请求客户端(请参阅 this answer FMI)中自己设置它,现在我得到了成功的响应。

我使用的相关片段:

var request = URLRequest(url:renderedUrl)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")