HTTP request using iOS

Example 1 – pass data using Header fields

On the server side, the receiving script will strip the information from the request object and use the data, query the database, and return it.

On our iOS client side, we create a NSURL object that takes in the string of the web api address that we want to hit.

For example in our case it would be

http://104.167.105.220:8080/users/(some email)

NSString –> NSURL –> NSMutableURLRequest

  • So first, we assemble a string to satisfy the HTTP url.
  • Then throw it in a NSURL.
  • Create a NSMutableURLRequest object, and init it with our NSURL.
  • Manipulate the request object with http header field/value combinations.
  • set the HTTP VERB

NSMutableURLRequest object (iOS)

NOTE:

The key/value you assign in your NSMutableURLRequest object will be read on the server side (node) on the request object’s header’s properties.

Reading values for HTTP Header fields (node)

After you have set up your URL Request object, you insert it into a NSURLConnection, and make it run.

NSURLConnection object (iOS)

Full Source

Example 2 – authenticate a user