AFHttpSessionManager - 导致org.codehaus.jackson.JsonParseException的多部分POST

时间:2014-07-09 18:47:42

标签: jackson afnetworking multipartform-data afnetworking-2

我试图使用AFNetworking 2的AFHTTPSessionManager将pdf内容发布到我的网络服务。我已经将AFHTTPSessionManager子类化,并将请求和响应序列化器设置为相应的AFJsonXXXSerializer。但是当我将表单数据作为多部分请求发布时,我得到一个JsonParseException。我很确定这是一个直接的用例,我可能会遗漏一些东西。请帮助,提前致谢!

[accessMgr postDataToURL:@"objects-d2" usingParams:nil fileData:imageData andDelegate:self];

调用以下方法POST

-(void) postDataToURL:(NSString *) urlString usingParams:(id)parameters fileData: (NSData          *)fileData andDelegate:(id<RemoteAccessDelegate>) remoteDelegate
{

//RESTSessionManager extends AFHTTPSessionMaanger
RESTSessionManager *manager = [self getManager:remoteDelegate];

[manager POST:urlString parameters: parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:fileData name:@"content" fileName:@"test.pdf" mimeType:@"application/pdf"];

请求包含以下标题和表单数据

POST /d2fs/repositories/ls67sp2/objects-d2 HTTP/1.1
Host: localhost:8080
Content-Type: multipart/form-data; boundary=Boundary+422AF86226B68040
Connection: keep-alive
Transfer-Encoding: Chunked
Accept: application/json
User-Agent: IIG Mobile/1.1 (iPad Simulator; iOS 7.1; Scale/2.00)
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 bHNhZG1pbjpsc2FkbWlu
Accept-Encoding: gzip, deflate

--Boundary+422AF86226B68040
Content-Disposition: form-data; name="content"; filename="test.pdf"
Content-Type: application/pdf

%PDF-1.3
%ƒÂÚÂÎßÛ†–ƒ∆
<more file content>

我已经验证了尾部边界也已设置。我从我的网络服务得到的回复是

{
    "status": 400,
    "code": "E_INPUT_ILLEGAL_ARGUMENTS",
    "message": "There are illegal arguments provided.",
    "details": "org.codehaus.jackson.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.commons.fileupload.MultipartStream$ItemInputStream@22a5cdce; line: 1, column: 2];Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.commons.fileupload.MultipartStream$ItemInputStream@22a5cdce; line: 1, column: 2]"
}

1 个答案:

答案 0 :(得分:0)

我今天能够解决这个问题。在附加文件数据之前,我必须附加一个json对象,该对象是关于该对象的元数据,并且上传作为一个魅力!感谢。

[formData appendPartWithFormData:data name:@"object"];
[formData appendPartWithFileData:fileData name:@"content" fileName:@"test.pdf" mimeType:@"application/pdf"];