Mashandisiro Ekushandisa CURL Kutumira API Zvikumbiro

Muchikamu chino, tiri kuenda kuzokurukura mashandisiro curl kusangana neRESTful APIs. curl chinhu chekuraira-mutsara chinoshandiswa kutumira zvikumbiro kune API.

API zvikumbiro zvinoumbwa nezvikamu zvina zvakasiyana:

  • Magumo. Iyi ndiyo URL yatinotumira zvikumbiro kwaari.
  • Maitiro eHTTP. Chiito chatinoda kuita. Nzira dzakajairika ndedze GET POST PUT DELETE uye PATCH
  • Misoro. Misoro iyo yatinoda kutumira pamwe nechikumbiro chedu, i.e. mvumo musoro.
  • Muviri. Iyo data yatinoda kutumira kune api.

curl Syntax

Iyo syntax ye curl rairo ndeiyi:

curl [options] [URL...]

Sarudzo dzatichafukidza mune ino posvo ndedzinotevera:

  • -X kana --request - HTTP nzira yekushandisa
  • -i kana --include - Sanganisira misoro yekupindura
  • -d kana --data - Iyo data inotumirwa kuAPI
  • -H kana --header - Chero mamwe mahedhiyo anotumirwa

HTTP GET

Iyo GET nzira yajaira fetch zviwanikwa kubva kuseva. Mu curl, iyo GET nzira ndiyo yakasarudzika nzira, saka hatidi kuitsanangura.

Muenzaniso:

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

GET Uine Query Parameter

Tinogona zvakare kutumira emibvunzo paramende pamwe ne curl GET chikumbiro.

Muenzaniso:

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

HTTP POST

Iyo POST nzira inoshandiswa ku gadzira chishandiso pane server.

Kutumira curl POST chikumbiro tinoshandisa sarudzo -X POST.

POST Fomu Dhata

Muenzaniso:

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

Nokusingaperi, curl inoshandisa Content-Type: application/x-www-form-urlencoded sezvo Content-Type musoro, saka hatidi kuutsanangura patinotumira fomu fomu.

POST JSON

Ku POST JSON ne curl isu tinofanirwa kudoma Content-Type se application/json.

Muenzaniso:

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

HTTP PUT

Iyo PUT nzira inoshandiswa ku kugadzirisa kana kutsiva chishandiso pane server. Iyo inotsiva rese data reiyo yakasarudzika sosi neakapihwa data rekukumbira

Ongorora:Kune PUT chikumbiro, isu tinofanirwa kupa rese data mumuviri wechikumbiro.

Kutumira curl PUT chikumbiro tinoshandisa sarudzo -X PUT.

Muenzaniso:

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

Ichi chiri pamusoro PUT chikumbiro chinotsiva yedu yatambo gadzira positi ne 'Nyowani post zita' uye 'Nyowani post muviri'

HTTP PATCH

Iyo PATCH nzira inoshandiswa kugadzira zvishoma zvinogadziridzwa kune zviwanikwa zviri pane server.

Ongorora:Kune chikumbiro chePATCH, hatifanirwe kupa data rese. Isu tinongotumira iyo data yatinoda yakavandudzwa.

Kutumira curl PATCH chikumbiro tinoshandisa sarudzo -X PATCH.

Muenzaniso:

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

Cherekedza maitiro atiri kungotumira iwo muviri ne 'Yakagadziridzwa post zvemukati' sezvo isu tiri kuita zvishoma chikamu kugadzirisa.

HTTP Bvisa

Iyo DELETE nzira inoshandiswa kubvisa sosi yakatsanangurwa kubva kuseva.

Kutumira curl Bvisa chikumbiro tinoshandisa sarudzo -X DELETE.

curl -X DELETE https://jsonplaceholder.typicode.com/posts/5 Ongorora:Iyo DELETE nzira haina muviri.

Kusimbiswa

Dzimwe nguva nzvimbo yekupedzisira yeAPI inodzivisa kupinda uye inongoshanda chete zvikumbiro kune vanovimbiswa uye vanobvumidzwa vashandisi. Kune izvi zvikumbiro, isu tinofanirwa kupa chiratidzo chekuwana mumusoro wechikumbiro.

Kutumira curl musoro, tinoshandisa: -H sarudzo.

Chikumbiro chinotevera chinotumira POST chikumbiro nemutakuri tokeni mumusoro:

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!' }'

Mhedziso

Mune ino posvo isu takadzidza maitiro ekutumira zvikumbiro zveHTTP (GET, POST, PUT, PATCH uye DELETE) kune API uchishandisa curl mirairo.