I am using http post method and passing base64econded string into xml. But it gives me 400 in response. XMl tag structure is valid. When I removed this base64encoded string from xml it works perfectly. But including it gives error(400). Would base64encoded string may contains any character that make my request invalid ? Any help is welcome.
Asked
Active
Viewed 1,807 times
1
-
What errors are you getting on the web server logs? – Lee Armstrong May 05 '11 at 10:45
2 Answers
2
This may be caused by your boundary definitions not being 100% correct, eg. using wrong
Content-type: multipart/form-data, boundary=XXboundaryXX
instead of the correct
Content-type: multipart/form-data; boundary=XXboundaryXX
It may also be your boundary delimiters in the body that are not using correct syntax, eg. using wrong
--XXboundaryXX\r\n
instead of the correct
\r\n--XXboundaryXX\r\n
The following snippet should be correct for posting xml data, even for som nitty gritty older apache servers:
[request setValue:USER_AGENT forHTTPHeaderField:@"User-Agent"];
[request setTimeoutInterval: 15];
[request setHTTPMethod:@"POST"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"xmlstring\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[xml dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
I have previously discovered error 400 if these delimiters were not 100% correct.

Claus Broch
- 9,212
- 2
- 29
- 38
1
Check your base64encoder. You can do it here I'm using solution from this discussion.
If it's all right with your encoder, I suppose you're incorrect paste it into your xml (maybe with slashes or other 'bad' symbols)