Sie sind auf Seite 1von 33

HTTP and JSON

Todi Adiyatmo, ST., M.I.T.


Examining HTTP Request
Under the hood
Curl
The popular command line tool to create web
request
JSON

File Example available on
~layanan_web/code/curl

Topic Outline
Before we learn about web service, its better
for us to learn how HTTP request really work
The protocol
The Header
The Respond
The method involved
HTTP Request

HTTP Request
Chrome is a mandatory browser in this class
Chrome developer tools (f12)
Analyze behind the hood
Request response
CSS Editing
Javascript debugger
Etc
Chrome Developer Tools







The request and response is done in HTTP
Web Server acting as Server and client
Command line tool providing library for data
transfer across various protocol
Available in linux and PHP PECL module
Curl is going to be one of your primary tool
when calling and Web Service API
Using HTTP Protocol to read data from
various language / System ! (Cross Platform)

Curl
Curl in action (HTML Data)
Curl in action (JSON Data)
Network Stack
Cross Platform API Service
We can write http request with different
Programming Language Easyly
JAVA
PHP
Ruby
ASP
Every Programming Language have build a
http library
HTTP Programming Library

Load your PHP Curl extension first

Curl in PHP

Result :

Examine the Request and Response !
Data.php (JSON Server)
JSON_Client.php (Client)
Simple Server - Client

Data.php

JSON_Client.php
Javascript Object Notation
Kalkulator
Atribut : merk, pemilik

JSON

JSON (2)

Other more complex example
Simple Structure
Can be eval() at javascript side, easier code
execution
Easy to read (when properly formatted)
Why JSON

Network Request

And response..

Common HTTP Status
2xx Success
3xx Redirect
4xx Client Error
5xx Server Error

http://en.wikipedia.org/wiki/List_of_HTTP_st
atus_codes

Common HTTP Status (2)
200 Ok
Should be in most response
201 Created
After creating RESTfull instance
202 Accepted
Upload file
204 No Content

Remember this code for later use !
2xx Code - Success
301 Move Permanently
Access google.com -> google.co.id
304 Not Modified
Cache mechanism


3xx Code - Redirect
400
Bad request ( broken request header bad network)
401
Unathorized
403
Forbidden ( access to restricted area)
404
Page not found
4xx Code Client Error
500
Server Error (When your server is broken)
501
Not implemented (API)
502
Bad gateway
508 Loop
Permanent loop (bad code)
5xx Server Error
The response code is important for the REST
web service were going to build
Remember the important code
200
301
403
404
501
Important Notes
http://secretgeek.net/json_3mins.asp
More at :

Das könnte Ihnen auch gefallen