Example on using AFNetworking to post XML? - ios

As a followup to this question, I cannot get this to work so I need to take a step back I think.
Can someone post some sample code on how to post XML to a rest service WITHOUT using params? Is it even possible? Basically I have an object with several properties, and I convert this to XML and then need to upload it. But it fails if there is an image converted to string in the XML. Without that it works fine.
Even a JSON example would be helpful...
Totally blocked here :P

Use NSMutableURLRequest -setHTTPBody:, and set the Content-Type HTTP header to application/xml.
If you're doing this consistently, you may want to consider subclassing AFHTTPClient to add an XML parameter serialization option.

Related

Using Car2go Api for iOS app

Hi I am trying to get the list of all car2go cars and use it for a test application, the only problem is I do not know how to get the information. I have gotten a consumer key and secret key but the problem is I am unsure how to get the file using the api and then once I get the file I don't know how I would read it since it is using xml.
The response from the html is on this site
http://code.google.com/p/car2go/wiki/vehicles_v2_1
once I get the file I don't know how I would read it since it is using xml
You'll need to parse the response to extract the data you want. There are a number of ways to do that. For example, you could use NSXMLParser (which is included in Cocoa Touch), or you could use the TouchXML library, or you could use libxml.

How to use NSURLSession to post some properties and a image

I have a node.js as backend server. And it has a post method accept a title param and a latitude and longitude param and a image. So, I am not only post a image but with other params together. In the node.js,
title -> uitextview's text
latitude & longitude - > cllocation
and a image.
the node.js corresponding method is a form.
How to use NSURLSessionUploadTask to post them to the server.
A file upload with additional parameters will be usually accomplished using a HTTP message whose content type equals multipart/form-data. For reference, see Form-based File Upload in HTML, RFC 1867.
A multipart/form-data body is composed of one or more "parts" which have each a disposition header and an optional content type header. One part contains the data of the file along with a corresponding content type header, and the other part (or several parts) contain the parameters. The part containing parameters, may have a content type of application/x-www-form-urlencoded where the body consists of the encoded parameters, or perhaps JSON or whatever is supported by the server. Each single parameter may also be represented as a separate part.
The difficulty here is the proper composition of the HTTP message whose body is a set of parts. Cocoa or iOS does not have direct support for composing a multipart form data message. That is, you may try to compose this message out of the parts yourself, by hand.
However this technique is elaborated and error prone, and if you strive to be correct regarding all and relevant HTTP and MIME specifications you need to read through virtually 100 RFCs and understand them thoroughly and then painstakingly apply that specification in the underlying implementation. (Please, do yourself a favor and don't try this!)
While at the end of the day, it may turn out that it is relatively easy to compose a message out of parts yourself (there are few examples here on SO which demonstrates this), there's also the tricky part when you want utilize NSStreams as "virtual" body of a part, say a file stream which you want to use, since creating a NSData object seems too costly regarding the amount of allocated memory.
So, when you can afford to compose the complete body (consisting of multiple parts) of the HTTP message into one NSData object, the effort to accomplish this may be moderately low. Otherwise, if you cannot hold the image data and the whole body into memory, I would strongly recommend to use a third party library which is capable to use NSStream as "data sources" for body parts.
How to compose a multipart/form-data message is described in more detail in my answer here. There are also countless related questions on SO.
For a third party library which supports to construct body parts using a NSStream as data source, take a look at AFNetworking, possibly also MKNetworkKit (without supporting NSURLSession yet)

objc POST file and string parameters

I am trying to POST a file, and some string params to a .ashx page on my server from my iPad app using objc.
I have found lots of tutorials on how to post a file using objc, and I already have code to post strings. But I cant work out how to combine the two, so I can send multiple values in a single POST?
Could anyone help me out with an example or two? I am trying to avoid using 3rd party frameworks, so something using NSURLRequest would be preferred.
Thanks,
Mike
You need to use a multipart submission, with boundary strings, etc. Here's some helper code: https://github.com/Goles/SimpleHttpRequest from the answer to this question: Sending multipart POST from iOS and reading parameters in PHP $_POST?

asidownloadcache for post request?

In my iOS app,I'm sending a post request using the asihttprequest methods and receiving json data.
i want to be able to cache the parsed json data if possible (so that i can directly extract and display in the tableview).
asidownloadcache is helpful only if i'm using GET method. So, i'd like to know my options(along with an example if possible)
Thanks a ton for the help, in advance..

how to parse XML with ASIHTTP

I am currently testing how to combine ios, php, mysql and xml. So far I have set up a database, got my php scripts working and am using them to request data from my external mysql database that then returns the results to my app using ASIHTTPRequest network wrappers.. what I am now trying to do is parse the xml that I am generating with my php scripts, Its a pretty easy example my php looks a little something like this
<?xml version="1.0"?>
<entries>
<code>3237554</code>
</entries>
All I am looking to parse is that number, In the past I have use NSXMLParse from the objective-c library however I was calling everything from an rss feed so the set up to acquiring the data was entirely different as it was not self generated etc. Due to the fact that I am using ASIHTTPRequest I am taking a guess here 'that I would hopefully like you to help me with' is that I should be grabbing my xml in
- (void)requestFinished:(ASIHTTPRequest *)request
which is pretty obvious because I am grabbing just basic text output from there as it is now.. So my question is what is the best way to start parsing this text thats coming in? is there a special xml parser library I am not aware of or can I use NSXMLParser some how?
//EDIT:: Working solution here
If the size of the returned xml is not that big, libxml2 is a nice convenient solution that I have used effectively.
Take a look at Matt's blog here --
http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html

Resources