Me Pehea te whakamahi CURL ki te Tukuna Tono API

I tenei tuhinga, ka korerohia me pehea te whakamahi curl ki te taunekeneke ki nga API RESTful. curl he mahinga raina whakahau ka taea te tuku ki te tuku tono ki tetahi API.

Ko nga tono API e wha nga waahanga rereke:

  • Te pito mutunga. Koinei te URL e tukuna atu ai e matou nga tono ki a matou.
  • Ko te tikanga HTTP. Te mahi e hiahia ana matou ki te mahi. Ko nga tikanga tino nui ko GET POST PUT DELETE me PATCH
  • Nga pane. Nga pane e hiahia ana matou ki te tuku atu me ta maatau tono, hei tauira pane whakamanatanga.
  • Te tinana. Nga raraunga e hiahia ana matou ki te tuku ki te api.

curl Syntax

Te wetereo mo te curl ko te whakahau:

curl [options] [URL...]

Ko nga waahanga ka tirohia e maatau i tenei pou ko:

  • -X ko --request - Te tikanga HTTP hei whakamahi
  • -i ko --include - Whakauruhia nga pane pane
  • -d ko --data - Nga raraunga hei tuku ki te API
  • -H ranei --header - He pane taapiri hei tuku

HTTP GET

Ko te tikanga GET whakamahia ai tiki he rauemi mai i te kaituku. I te curl, ko te tikanga GET te tikanga taunoa, no reira kaore e hiahiatia ana e taatau te whakarite.

Tauira:



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

GET Me nga Tawhā Uiui

Ka taea hoki e taatau te tuku taatai ​​paatai ​​me nga curl KIA tono.

Tauira:

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

HTTP POST

Ko te tikanga POST e whakamahia ana ki hanga he rauemi kei runga i te kaituku.

Hei tuku i te curl Te tono POST ka whakamahia e matou te waahanga -X POST.

POST Puka Raraunga

Tauira:

curl -X POST -d 'userId=5&title=Post Title&body=Post content.' https://jsonplaceholder.typicode.com/posts

Na te taunoa, curl whakamahi Content-Type: application/x-www-form-urlencoded rite te Content-Type pane, na reira kaore e tika ana kia whakaputahia e matou i te wa e tuku tuhinga puka ana.

POST JSON

Ki TE PANUI i te JSON na curl me whakarite e matou te Content-Type hei application/json.

Tauira:

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

HTTP PUT

Ko te tikanga PUT ka whakamahia ki whakahou ranei whakakapi he rauemi kei runga i te kaituku. Ka whakakapihia e ia nga raraunga katoa o te rauemi kua whakaputahia me nga raraunga tono kua tohaina.

Panui:Mo te tono PUT, me whakarato e matou nga raraunga katoa i te roopu tono.

Hei tuku i te curl Tono PUT ka whakamahia e matou te waahanga -X PUT.

Tauira:

curl -X PUT -H 'Content-Type: application/json'
-d '{'userId': 5, 'title': 'New Post Title', 'body': 'New post content.'}'
https://jsonplaceholder.typicode.com/posts/5

Ko te tono PUT i runga ake nei ka whakakapi i ta maatau pou i hangaia i mua me te 'Taitara pou hou' me te 'Tuku pou hou'.

HTTP PATCH

Ko te tikanga PATCH e whakamahia ana hei hanga whakahoutanga waahanga ki te rauemi kei runga i te kaituku.

Panui:Mo te tono PATCH, kaore e hiahiatia te whakarato i nga raraunga katoa. Ka tukuna noa e maatau nga korero e hiahia ana matou kia whakahoutia.

Hei tuku i te curl Tono PATCH ka whakamahia e matou te waahanga -X PATCH.

Tauira:

curl -X PATCH -H 'Content-Type: application/json'
-d '{'userId': 5, 'body': 'Updated post content.'}'
https://jsonplaceholder.typicode.com/posts/5

Tirohia me pehea e tukuna atu ana e matou te tinana me nga 'Ihirangi pou kua whakahoutia' i a matou e mahi ana i tetahi waahanga whakahou.

HTTP MUTU

Ko te tikanga MUTU ka whakamahia hei tango i nga rauemi kua tohua mai i te kaituku.

Hei tuku i te curl MURI tono ka whakamahia e matou te whiringa -X DELETE.

curl -X DELETE https://jsonplaceholder.typicode.com/posts/5 Panui:Ko te tikanga MUTU kaore he tinana.

Motuhēhēnga

I etahi wa ko te mutunga o te API he aukati i te urunga atu ka tukuna noa nga tono ki nga kaiwhakamahi whaimana me nga tangata whai mana. Mo enei tono, me whakarato e matou he tohu whakauru ki te pane o te tono.

Hei tuku i te curl pane, ka whakamahia e matou: -H whiringa.

Ko te tono e whai ake nei ka tukuna he tono POST me te tohu kaikawe i te pane.

curl -X POST https://some-web-url/api/v1/users -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'Authorization: Bearer {ACCESS_TOKEN}' -H 'cache-control: no-cache' -d '{ 'username' : 'myusername', 'email' : 'myusername@gmail.com', 'password' : 'Passw0rd123!' }'

Whakamutunga

I tenei panui i akohia e maatau te tuku tono HTTP (GET, POST, PUT, PATCH me te MUKU) ki tetahi API ma te whakamahi i nga whakahau aukati.