简体   繁体   中英

415 Unsupported Media Type in Restkit response

I'm trying to call a rest web service in an iOS application using Restkit but I get this error : restkit.network:RKObjectRequestOperation.m:210 response.body=415 Unsupported Media Type

415 Unsupported Media Type

I puted this line in my code to set the content type as application/json:

request.headers={
    Accept = "application/json";
    "Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
    Authorization = "Basic U1lTQURNSU46aHVsNTU4ODg1OA==";
    "Content-Type" = "application/json; charset=utf-8";
    "User-Agent" = "iosProj/1 (iPad Simulator; iOS 8.3; Scale/2.00)";
}

and this is my request shown in the console

 request.headers={ Accept = "application/json"; "Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5"; Authorization = "Basic U1lTQURNSU46aHVsNTU4ODg1OA=="; "Content-Type" = "application/json; charset=utf-8"; "User-Agent" = "iosProj/1 (iPad Simulator; iOS 8.3; Scale/2.00)"; } 

Then I found that the problem could be caused by "charset=utf-8" as mentioned here . My question is how to remove "charset=utf-8" in Restkit

Thanks to @Wain comment I solved the problem by creating the request and setting the content type header :

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];    
[request setHTTPBody:jsonData];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor
                                            responseDescriptorWithMapping: [LoginResponse getResponseMapping]
                                            method:RKRequestMethodPOST
                                            pathPattern:nil
                                            keyPath:@"OutputParameters"
                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];

I hope this helps someone. This happened recently to me and I was missing Content-Type as well. However you can easily add that to RKObjectManager as:

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:YOUR_BASE_URL]];
[manager setRequestSerializationMIMEType:RKMIMETypeJSON];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM