[英]Http post request works fine in postman but not in iOS?
I'm using RestKit to making network request.我正在使用 RestKit 进行网络请求。 I'm using basic auth.
我正在使用基本身份验证。 I'm trying to make post call to server.
我正在尝试对服务器进行后期调用。 It's giving me 401 error.
它给了我 401 错误。 But i took same request body and header tried same request in postman and It's working fine.
但是我采用了相同的请求正文和标头,在邮递员中尝试了相同的请求,并且工作正常。
code in 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: "")
}
)
post man code邮递员代码
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"}
I had a similar issue.我有一个类似的问题。 Request body from Swift was identical to the Postman request and I was getting Bad Request responses from the Django Rest Framework API host.
来自 Swift 的请求正文与 Postman 请求相同,我从 Django Rest Framework API 主机收到了错误的请求响应。
Postman adds the Content-Type
header with value application/json
if that's the body type you've indicated.如果这是您指定的正文类型,邮递员会添加值为
application/json
的Content-Type
标头。 I tried setting it myself from within my Swift iOS request client (see this answer FMI) and I'm now getting successful responses.我尝试从我的 Swift iOS 请求客户端中自己设置它(请参阅此答案FMI),现在我得到了成功的响应。
The relevant snippet I used:我使用的相关片段:
var request = URLRequest(url:renderedUrl)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.