Test a REST API with curl คืออะไร

Application Program Interface (API) คือชุดของคำจำกัดความและโปรโตคอลที่ช่วยให้โปรแกรมซอฟต์แวร์สามารถสื่อสารกันได้ คำว่า REST หมายถึงการถ่ายโอนสถานะที่เป็นตัวแทน เป็นรูปแบบสถาปัตยกรรมที่ประกอบด้วยชุดของข้อจำกัดที่จะใช้ในการสร้างบริการเว็บ

1. Overview

RESTful API เป็น API ที่เป็นไปตามสถาปัตยกรรม REST โดยทั่วไป REST API จะใช้โปรโตคอล HTTP สำหรับการส่งและดึงข้อมูลและการตอบกลับในรูปแบบ JSON คุณสามารถใช้เมธอด HTTP มาตรฐานเพื่อสร้าง ดู อัปเดต หรือลบทรัพยากรผ่าน API

หากต้องการทดสอบและโต้ตอบกับ RESTful API คุณสามารถใช้ไลบรารีหรือเครื่องมือใดก็ได้ที่ส่งคำขอ HTTP

API requests are made up of four different parts:

  • Endpoint คือ URL ที่ไคลเอ็นต์ใช้ในการสื่อสารกับเซิร์ฟเวอร์

วิธี HTTP มันบอกเซิร์ฟเวอร์ว่าลูกค้าต้องการทำอะไร วิธีที่พบบ่อยที่สุดคือ GET POST PUT DELETE และ PATCH

  • ส่วนหัว ใช้เพื่อส่งข้อมูลเพิ่มเติมระหว่างเซิร์ฟเวอร์และไคลเอนต์ เช่น authorization
  • The body. The data sent to the server.

ในบทความนี้ เราจะพูดถึงวิธีใช้ curl เพื่อโต้ตอบกับ RESTful API curl เป็นยูทิลิตี้บรรทัดคำสั่งสำหรับการถ่ายโอนข้อมูลจากหรือไปยังเซิร์ฟเวอร์ระยะไกล มันถูกติดตั้งโดยค่าเริ่มต้นบน macOS และลีนุกซ์ส่วนใหญ่


2.Curl Options

The syntax for the curl command is as follows:

curl [options] [URL...]

Here are the options that we’ll use when making requests:

  • -X, --request - The HTTP method to be used.
  • -i, --include - Include the response headers.
  • -d, --data - The data to be sent.
  • -H, --header - Additional header to be sent.


3.HTTP GET

The GET method requests a specific resource from the server.

GET is the default method when making HTTP requests with curl. Here is an example of making a GET request to the JSONPlaceholder API to a JSON representation of all posts:

curl https://jsonplaceholder.typicode.com/posts

To filter the results use query params:

curl https://jsonplaceholder.typicode.com/posts?userId=1


4.HTTP POST

วิธีการ POST ใช้เพื่อสร้างทรัพยากรบนเซิร์ฟเวอร์ หากมีทรัพยากรอยู่ จะถูกแทนที่คำสั่งต่อไปนี้สร้างคำขอ POST โดยใช้ข้อมูลที่ระบุด้วยอ็อพชัน -d:

curl -X POST -d "userId=5&title=Hello World&body=Post body." https://jsonplaceholder.typicode.com/posts

The type of the request body is specified using the Content-Type header. By default when this header is not given curl uses Content-Type: application/x-www-form-urlencoded.

To send a JSON formatted data set the body type to application/json

curl -X POST -H "Content-Type: application/json" \
    -d '{"userId": 5, "title": "Hello World", "body": "Post body."}' \
    https://jsonplaceholder.typicode.com/posts

Run on ssh

curl -H 'x-api-key: xxxx' -d '{""}' https://URL


5.HTTP PUT

เมธอด PUT ใช้เพื่ออัพเดตหรือแทนที่ทรัพยากรบนเซิร์ฟเวอร์ โดยจะแทนที่ข้อมูลทั้งหมดของทรัพยากรที่ระบุด้วยข้อมูลคำขอ

curl -X PUT -d "userId=5&title=Hello World&body=Post body." https://jsonplaceholder.typicode.com/posts/5


6.HTTP PATCH

เมธอด PUT ใช้เพื่ออัปเดตทรัพยากรบางส่วนบนเซิร์ฟเวอร์

curl -X PUT -d "title=Hello Universe" https://jsonplaceholder.typicode.com/posts/5


7.HTTP DELETE

วิธีการ DELETE จะลบทรัพยากรที่ระบุออกจากเซิร์ฟเวอร์

curl -X DELETE https://jsonplaceholder.typicode.com/posts/5


8.Authentication

หากปลายทาง API ต้องมีการตรวจสอบสิทธิ์ คุณจะต้องขอรับคีย์การเข้าถึง มิฉะนั้น เซิร์ฟเวอร์ API จะตอบกลับด้วยข้อความตอบกลับ "Access Forbidden" หรือ "Unauthorized"

กระบวนการรับคีย์การเข้าถึงขึ้นอยู่กับ API ที่คุณใช้ เมื่อคุณมีโทเค็นการเข้าถึงแล้ว คุณสามารถส่งไปที่ส่วนหัว:

curl -X GET -H "Authorization: Bearer {ACCESS_TOKEN}" "https://api.server.io/posts"


0
270